/* * Projecte Fressa a JAVA * TFormFont.java * Created on 7 / novembre / 2008, 23:09 * * @author Jordi Lagares Roset "jlagares@xtec.cat - www.lagares.org" * amb el suport del Departament d'Educacio de la Generalitat de Catalunya * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details (see the LICENSE file). */ import java.awt.event.ItemEvent; import javax.swing.*; import java.awt.*; import javax.swing.event.ListSelectionEvent; import javax.swing.event.*; import java.awt.event.*; public class TFormFont extends javax.swing.JDialog { public javax.swing.JLabel jLabel1; public javax.swing.JLabel jLabel2; public javax.swing.JLabel jLabel3; public javax.swing.JLabel jLabelMostra; private javax.swing.JTextField EditColor; private javax.swing.JTextField EditFont; private javax.swing.JTextField EditEstilFont; private javax.swing.JTextField EditGrandaria; private javax.swing.JScrollPane ScrollPane1JListFonts; private javax.swing.JList JListFonts; private javax.swing.JScrollPane ScrollPane1JListEstilFont; private javax.swing.JList JListEstilFont; private javax.swing.JScrollPane ScrollPane1JListGrandaria; private javax.swing.JList JListGrandaria; public javax.swing.JComboBox ComboBoxColor; public javax.swing.JButton jButtonDAcord; public javax.swing.JButton jButtonCancelar; private Tools tool; public int FontSize; public int FontColor; public String FontName; public int FontStyle; public int resultat = 0; private Timer TimerEsperarListenerReturnEsc; int TimerEsperarListenerReturnEscInterval = 100; public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new TFormFont(new javax.swing.JFrame(), true).setVisible(true); } }); } public TFormFont(java.awt.Frame parent, boolean modal) { super(parent, modal); initComponents(); } public TFormFont(javax.swing.JDialog parent, boolean modal) { super(parent, modal); initComponents(); } private void initComponents() { jLabel1 = new javax.swing.JLabel(); jLabel1.setFont(new Font("sansserif",0,11)); jLabel2 = new javax.swing.JLabel(); jLabel2.setFont(new Font("sansserif",0,11)); jLabel3 = new javax.swing.JLabel(); jLabel3.setFont(new Font("sansserif",0,11)); jLabelMostra = new javax.swing.JLabel(); EditColor = new javax.swing.JTextField(); EditFont = new javax.swing.JTextField(); EditEstilFont = new javax.swing.JTextField(); EditGrandaria = new javax.swing.JTextField(); JListFonts = new javax.swing.JList(); JListEstilFont = new javax.swing.JList(); JListGrandaria = new javax.swing.JList(); ComboBoxColor = new javax.swing.JComboBox(); jButtonDAcord = new javax.swing.JButton(); jButtonCancelar = new javax.swing.JButton(); tool = new Tools(); FontStyle=0; setSize(387,280); getContentPane().setLayout(null); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setTitle("Font"); setResizable(false); jLabel1.setText("Font:"); jLabel1.setBounds(10,10,160,13); getContentPane().add(jLabel1); EditFont.setBounds(10,25,160,21); EditFont.setEnabled(false); getContentPane().add(EditFont); String FontList[]; FontList=GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); JListFonts.setListData(FontList); JListFonts.addListSelectionListener(new javax.swing.event.ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { EditFont.setText(JListFonts.getSelectedValue().toString()); //jLabelMostra.setFont(new Font(EditFont.getText(),jLabelMostra.getFont().getStyle(),jLabelMostra.getFont().getSize())); jLabelMostra.setFont(new Font(EditFont.getText(),FontStyle,tool.StrToInt(EditGrandaria.getText()))); } }); ScrollPane1JListFonts = new javax.swing.JScrollPane(); ScrollPane1JListFonts.setBounds(10,48,160,120); ScrollPane1JListFonts.setViewportView(JListFonts); getContentPane().add(ScrollPane1JListFonts); jLabel2.setText("Estil de la Font:"); jLabel2.setBounds(180,10,100,13); getContentPane().add(jLabel2); EditEstilFont.setBounds(180,25,100,21); EditEstilFont.setEnabled(false); getContentPane().add(EditEstilFont); String EstilFontList[] = new String[4]; EstilFontList[0]="Normal"; EstilFontList[1]="Negreta"; EstilFontList[2]="Cursiva"; EstilFontList[3]="Negreta cursiva"; JListEstilFont.setListData(EstilFontList); JListEstilFont.addListSelectionListener(new javax.swing.event.ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { EditEstilFont.setText(JListEstilFont.getSelectedValue().toString()); if (EditEstilFont.getText().equals("Normal")) { FontStyle=0; } else if (EditEstilFont.getText().equals("Negreta")) { FontStyle=Font.BOLD; } else if (EditEstilFont.getText().equals("Cursiva")) { FontStyle=Font.ITALIC ; } else if (EditEstilFont.getText().equals("Negreta cursiva")) { FontStyle=Font.BOLD+Font.ITALIC; } jLabelMostra.setFont(new Font(EditFont.getText(),FontStyle,tool.StrToInt(EditGrandaria.getText()))); } }); ScrollPane1JListEstilFont = new javax.swing.JScrollPane(); ScrollPane1JListEstilFont.setBounds(180,48,100,120); ScrollPane1JListEstilFont.setViewportView(JListEstilFont); getContentPane().add(ScrollPane1JListEstilFont); jLabel3.setText("Grand\u00e0ria:"); jLabel3.setBounds(290,10,80,13); getContentPane().add(jLabel3); EditGrandaria.setBounds(290,25,80,21); EditGrandaria.setEnabled(false); getContentPane().add(EditGrandaria); String ListGrandaria[] = new String[16]; ListGrandaria[0]="8"; ListGrandaria[1]="9"; ListGrandaria[2]="10"; ListGrandaria[3]="11"; ListGrandaria[4]="12"; ListGrandaria[5]="14"; ListGrandaria[6]="16"; ListGrandaria[7]="18"; ListGrandaria[8]="20"; ListGrandaria[9]="22"; ListGrandaria[10]="24"; ListGrandaria[11]="26"; ListGrandaria[12]="28"; ListGrandaria[13]="36"; ListGrandaria[14]="48"; ListGrandaria[15]="72"; JListGrandaria.setListData(ListGrandaria); JListGrandaria.addListSelectionListener(new javax.swing.event.ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { EditGrandaria.setText(JListGrandaria.getSelectedValue().toString()); jLabelMostra.setFont(new Font(EditFont.getText(),FontStyle,tool.StrToInt(EditGrandaria.getText()))); } }); ScrollPane1JListGrandaria = new javax.swing.JScrollPane(); ScrollPane1JListGrandaria.setBounds(290,48,80,120); ScrollPane1JListGrandaria.setViewportView(JListGrandaria); getContentPane().add(ScrollPane1JListGrandaria); jLabelMostra.setText("AaBbCc"); jLabelMostra.setBounds(10,170,220,100); getContentPane().add(jLabelMostra); EditFont.setText(jLabelMostra.getFont().getFontName()); if (jLabelMostra.getFont().isPlain()) { EditEstilFont.setText("Normal"); } else if ((jLabelMostra.getFont().isItalic()) && (jLabelMostra.getFont().isBold())) { EditEstilFont.setText("Negreta cursiva"); } if (jLabelMostra.getFont().isBold()) { EditEstilFont.setText("Negreta"); } else if (jLabelMostra.getFont().isItalic()) { EditEstilFont.setText("Cursiva"); } EditGrandaria.setText(tool.IntToStr(jLabelMostra.getFont().getSize())); EditColor.setBounds(150,175,50,21); EditColor.setEditable(false); getContentPane().add(EditColor); //ComboBoxColor.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Negre", "Marr\u00f3", "Verd", "Verd oliva", "Blau mari", "Porpra", "Turquesa", "Gris", "Plata", "Vermell", "Llimona", "Groc", "Blau", "F\u00facsia", "Blau cel", "Blanc"})); ComboBoxColor.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Black", "Blue", "Cyan", "Dark gray", "Gray", "Green", "Light gray", "Magenta", "Orange", "Pink", "Red", "White", "Yellow"})); ComboBoxColor.setBounds(205,175,70,21); getContentPane().add(ComboBoxColor); ComboBoxColor.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { CanviarColorFont(); } }); //jButtonDAcord.setText("D'acord"); jButtonDAcord.setIcon(new javax.swing.ImageIcon(this.getClass().getResource("dibuixos/catala/ok.gif"))); jButtonDAcord.setBounds(280,175,90,33); jButtonDAcord.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); getContentPane().add(jButtonDAcord); jButtonDAcord.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonDAcordClick(); } }); //jButtonCancelar.setText("Cancel.lar"); jButtonCancelar.setIcon(new javax.swing.ImageIcon(this.getClass().getResource("dibuixos/catala/cancel.gif"))); jButtonCancelar.setBounds(280,215,90,33); jButtonCancelar.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); getContentPane().add(jButtonCancelar); jButtonCancelar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonCancelarClick(); } }); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(WindowEvent e) { Toolkit.getDefaultToolkit().removeAWTEventListener(ListenerReturnEsc); } }); //He hagut de posar aquest timer perque l'inici del ListenerReturnEsc fos mes tard ja que si veniem aqui amb un return no s'obria el quadre de dialeg ja que el mateix return la tencava ActionListener TaskPerformerTimerEsperarListenerReturnEscTimer=new ActionListener() { public void actionPerformed(ActionEvent e){ TimerEsperarListenerReturnEscTimer(); } }; TimerEsperarListenerReturnEsc = new Timer(TimerEsperarListenerReturnEscInterval,TaskPerformerTimerEsperarListenerReturnEscTimer); //TimerEsperarListenerReturnEsc.start(); } public void TimerEsperarListenerReturnEscTimer() { TimerEsperarListenerReturnEsc.stop(); Toolkit.getDefaultToolkit().addAWTEventListener(ListenerReturnEsc,AWTEvent.KEY_EVENT_MASK); } private AWTEventListener ListenerReturnEsc = new AWTEventListener() { public void eventDispatched(AWTEvent event) { KeyEvent ke = (KeyEvent)event; if (ke.getID() == KeyEvent.KEY_TYPED) { if (ke.getKeyChar() == KeyEvent.VK_ESCAPE) { jButtonCancelarClick(); } else if (ke.getKeyChar() == KeyEvent.VK_ENTER) { //jButtonDAcordClick(); if (Global.EsWindowsLookAndFeel==1) { if (jButtonCancelar.isFocusOwner()) { } else { jButtonDAcordClick(); } } else { if (jButtonCancelar.isFocusOwner()) { jButtonCancelarClick() ; } else { jButtonDAcordClick(); } } } } } }; public void Activar() { EditFont.setText(FontName); if (FontStyle==0) { EditEstilFont.setText("Normal"); } if (FontStyle==1) { EditEstilFont.setText("Negreta"); } if (FontStyle==2) { EditEstilFont.setText("Cursiva"); } if (FontStyle==3) { EditEstilFont.setText("Negreta cursiva"); } EditGrandaria.setText(tool.IntToStr(FontSize)); int RedColor=(int)(FontColor%256); FontColor=FontColor-RedColor; FontColor=(int)(FontColor/256); int GreenColor=(int)(FontColor%256); FontColor=FontColor-GreenColor; int BlueColor=(int)(FontColor/256); Color c =new Color(RedColor,GreenColor,BlueColor); ComboBoxColor.setSelectedIndex(-1); jLabelMostra.setForeground(c); EditColor.setBackground(c); Color lc[] = new Color[13]; lc[0]=Color.BLACK; lc[1]=Color.BLUE; lc[2]=Color.CYAN; lc[3]=Color.DARK_GRAY; lc[4]=Color.GRAY; lc[5]=Color.GREEN; lc[6]=Color.LIGHT_GRAY; lc[7]=Color.MAGENTA; lc[8]=Color.ORANGE; lc[9]=Color.PINK; lc[10]=Color.RED; lc[11]=Color.WHITE; lc[12]=Color.YELLOW; for (int i=0; i<=ComboBoxColor.getItemCount()-1;i++) { if (c.equals(lc[i])) { ComboBoxColor.setSelectedIndex(i); break; } } JListFonts.setSelectedIndex(-1); for (int i=0; i<=JListFonts.getModel().getSize()-1;i++) { if (JListFonts.getModel().getElementAt(i).equals(EditFont.getText())) { JListFonts.setSelectedIndex(i); break; } } JListEstilFont.setSelectedIndex(-1); for (int i=0; i<=JListEstilFont.getModel().getSize()-1;i++) { if (JListEstilFont.getModel().getElementAt(i).equals(EditEstilFont.getText())) { JListEstilFont.setSelectedIndex(i); break; } } JListGrandaria.setSelectedIndex(-1); for (int i=0; i<=JListGrandaria.getModel().getSize()-1;i++) { if (JListGrandaria.getModel().getElementAt(i).equals(EditGrandaria.getText())) { JListGrandaria.setSelectedIndex(i); break; } } TimerEsperarListenerReturnEsc.start(); } private void CanviarColorFont() { Color c=Color.BLACK; if (ComboBoxColor.getSelectedIndex()==0) { c=Color.BLACK; } if (ComboBoxColor.getSelectedIndex()==1) { c=Color.BLUE; } if (ComboBoxColor.getSelectedIndex()==2) { c=Color.CYAN; } if (ComboBoxColor.getSelectedIndex()==3) { c=Color.DARK_GRAY; } if (ComboBoxColor.getSelectedIndex()==4) { c=Color.GRAY; } if (ComboBoxColor.getSelectedIndex()==5) { c=Color.GREEN; } if (ComboBoxColor.getSelectedIndex()==6) { c=Color.LIGHT_GRAY; } if (ComboBoxColor.getSelectedIndex()==7) { c=Color.MAGENTA; } if (ComboBoxColor.getSelectedIndex()==8) { c=Color.ORANGE; } if (ComboBoxColor.getSelectedIndex()==9) { c=Color.PINK; } if (ComboBoxColor.getSelectedIndex()==10) { c=Color.RED; } if (ComboBoxColor.getSelectedIndex()==11) { c=Color.WHITE; } if (ComboBoxColor.getSelectedIndex()==12) { c=Color.YELLOW; } jLabelMostra.setForeground(c); EditColor.setBackground(c); } private void jButtonCancelarClick() { resultat=0; Toolkit.getDefaultToolkit().removeAWTEventListener(ListenerReturnEsc); setVisible(false); } private void jButtonDAcordClick() { jLabelMostra.setFont(new Font(EditFont.getText(),FontStyle,tool.StrToInt(EditGrandaria.getText()))); FontName=EditFont.getText(); FontStyle=FontStyle; FontSize=tool.StrToInt(EditGrandaria.getText()); int Red=jLabelMostra.getForeground().getRed(); int Blue=jLabelMostra.getForeground().getBlue(); int Green=jLabelMostra.getForeground().getGreen(); FontColor=Red+Green*256+Blue*256*256; resultat=1; Toolkit.getDefaultToolkit().removeAWTEventListener(ListenerReturnEsc); setVisible(false); } public void ShowModalCentered() { Toolkit.getDefaultToolkit().addAWTEventListener(ListenerReturnEsc,AWTEvent.KEY_EVENT_MASK); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = this.getSize(); if (frameSize.height > screenSize.height) frameSize.height = screenSize.height; if (frameSize.width > screenSize.width) frameSize.width = screenSize.width; this.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); this.setVisible(true); } }