1 22 23 package org.gjt.sp.jedit.gui; 24 25 import javax.swing.*; 27 import java.awt.event.*; 28 import java.awt.*; 29 import org.gjt.sp.jedit.jEdit; 30 import org.gjt.sp.jedit.GUIUtilities; 31 import org.gjt.sp.jedit.OperatingSystem; 32 34 42 public class ColorWellButton extends JButton 43 { 44 public ColorWellButton(Color color) 46 { 47 setIcon(new ColorWell(color)); 48 setMargin(new Insets(2,2,2,2)); 49 addActionListener(new ActionHandler()); 50 51 if(OperatingSystem.isMacOSLF()) 53 putClientProperty("JButton.buttonType","toolbar"); 54 } 56 public Color getSelectedColor() 58 { 59 return ((ColorWell)getIcon()).color; 60 } 62 public void setSelectedColor(Color color) 64 { 65 ((ColorWell)getIcon()).color = color; 66 repaint(); 67 } 69 static class ColorWell implements Icon 71 { 72 Color color; 73 74 ColorWell(Color color) 75 { 76 this.color = color; 77 } 78 79 public int getIconWidth() 80 { 81 return 35; 82 } 83 84 public int getIconHeight() 85 { 86 return 10; 87 } 88 89 public void paintIcon(Component c, Graphics g, int x, int y) 90 { 91 if(color == null) 92 return; 93 94 g.setColor(color); 95 g.fillRect(x,y,getIconWidth(),getIconHeight()); 96 g.setColor(color.darker()); 97 g.drawRect(x,y,getIconWidth()-1,getIconHeight()-1); 98 } 99 } 101 class ActionHandler implements ActionListener 103 { 104 public void actionPerformed(ActionEvent evt) 105 { 106 JDialog parent = GUIUtilities.getParentDialog(ColorWellButton.this); 107 JDialog dialog; 108 if (parent != null) 109 { 110 dialog = new ColorPickerDialog(parent, 111 jEdit.getProperty("colorChooser.title"), 112 true); 113 } 114 else 115 { 116 dialog = new ColorPickerDialog( 117 JOptionPane.getFrameForComponent( 118 ColorWellButton.this), 119 jEdit.getProperty("colorChooser.title"), 120 true); 121 } 122 dialog.pack(); 123 dialog.setVisible(true); 124 } 125 } 127 133 private class ColorPickerDialog extends EnhancedDialog implements ActionListener 134 { 135 public ColorPickerDialog(Frame parent, String title, boolean modal) 136 { 137 super(parent,title,modal); 138 139 init(); 140 } 141 142 public ColorPickerDialog(Dialog parent, String title, boolean modal) 143 { 144 super(parent,title,modal); 145 146 getContentPane().setLayout(new BorderLayout()); 147 148 init(); 149 } 150 151 public void ok() 152 { 153 Color c = chooser.getColor(); 154 if (c != null) 155 setSelectedColor(c); 156 setVisible(false); 157 } 158 159 public void cancel() 160 { 161 setVisible(false); 162 } 163 164 public void actionPerformed(ActionEvent evt) 165 { 166 if (evt.getSource() == ok) 167 ok(); 168 else 169 cancel(); 170 } 171 172 private JColorChooser chooser; 174 private JButton ok; 175 private JButton cancel; 176 177 private void init() 178 { 179 Color c = getSelectedColor(); 180 if(c == null) 181 chooser = new JColorChooser(); 182 else 183 chooser = new JColorChooser(c); 184 185 getContentPane().add(BorderLayout.CENTER, chooser); 186 187 Box buttons = new Box(BoxLayout.X_AXIS); 188 buttons.add(Box.createGlue()); 189 190 ok = new JButton(jEdit.getProperty("common.ok")); 191 ok.addActionListener(this); 192 buttons.add(ok); 193 buttons.add(Box.createHorizontalStrut(6)); 194 getRootPane().setDefaultButton(ok); 195 cancel = new JButton(jEdit.getProperty("common.cancel")); 196 cancel.addActionListener(this); 197 buttons.add(cancel); 198 buttons.add(Box.createGlue()); 199 200 getContentPane().add(BorderLayout.SOUTH, buttons); 201 pack(); 202 setLocationRelativeTo(getParent()); 203 } } } 206 | Popular Tags |