View Javadoc
1 /* ------------------------------------------------------------------- 2 * Java source file for the class BorderChooser 3 * 4 * Copyright (c), 2002, Masahiro Takatsuka. 5 * All Rights Researved. 6 * 7 * Original Author: Masahiro Takatsuka (masa@jbeans.net) 8 * $Author: takatsukam $ 9 * 10 * $Date: 2003/07/25 04:51:44 $ 11 * 12 * $Id: BorderChooser.java,v 1.1.1.1 2003/07/25 04:51:44 takatsukam Exp $ 13 * 14 * Reference: Document no: 15 * ___ ___ 16 * 17 * To Do: 18 * ___ 19 * 20 ------------------------------------------------------------------- */ 21 22 /* --------------------------- Package ---------------------------- */ 23 package net.jbeans.bean.property.editor; 24 25 /* ------------------ Import classes (packages) ------------------- *//package-summary/html">color="#329900"> ------------------ Import classes (packages) ------------------- *//package-summary.html">color="#329900">/* ------------------ Import classes (packages) ------------------- *//package-summary.html">color="#329900"> ------------------ Import classes (packages) ------------------- */ 26 import java.awt.*; 27 import java.awt.event.*; 28 import java.util.*; 29 import javax.swing.*; 30 import javax.swing.border.*; 31 32 /*==================================================================== 33 Implementation of class BorderChooser 34 ====================================================================*/ 35 /*** 36 * generally describe BorderChooser in here 37 * 38 * @version $Revision: 1.1.1.1 $ 39 * @author Masahiro Takatsuka (masa@jbeans.net) 40 * @see JTabbedPane 41 */ 42 43 final class BorderChooser extends JTabbedPane { 44 private Border border = null; 45 private static Color sColor = null; 46 private static JPanel sPrevious = null; 47 /* this guy is special..title needs to be set */ 48 private Vector titledBordersList = null; 49 private JPanel titledBorders = null; 50 51 BorderChooser() { 52 Border blackline, etched, raisedbevel, loweredbevel, empty; 53 54 //A border that puts 10 extra pixels at the sides and 55 //bottom of each pane. 56 Border paneEdge = BorderFactory.createEmptyBorder(0,10,10,10); 57 58 blackline = BorderFactory.createLineBorder(Color.black); 59 etched = BorderFactory.createEtchedBorder(); 60 raisedbevel = BorderFactory.createRaisedBevelBorder(); 61 loweredbevel = BorderFactory.createLoweredBevelBorder(); 62 empty = BorderFactory.createEmptyBorder(); 63 64 /* ------------------ First pane: simple borders ------------------ */ 65 JPanel simpleBorders = new JPanel(); 66 simpleBorders.setBorder(paneEdge); 67 simpleBorders.setLayout(new BoxLayout(simpleBorders, 68 BoxLayout.Y_AXIS)); 69 70 addCompForBorder(empty, "Empty", 71 simpleBorders); 72 addCompForBorder(blackline, "Line", 73 simpleBorders); 74 addCompForBorder(etched, "Etched", 75 simpleBorders); 76 addCompForBorder(raisedbevel, "Raised Bevel", 77 simpleBorders); 78 addCompForBorder(loweredbevel, "Lowered Bevel", 79 simpleBorders); 80 81 /* ------------------ Second pane: matte borders ------------------ */ 82 JPanel matteBorders = new JPanel(); 83 matteBorders.setBorder(paneEdge); 84 matteBorders.setLayout(new BoxLayout(matteBorders, 85 BoxLayout.Y_AXIS)); 86 87 //XXX: We *should* size the component so that the icons tile OK. 88 //XXX: Without that, the icons are likely to be cut off and look bad. 89 ImageIcon icon = new ImageIcon("images/left.gif"); //20x22 90 Border border = BorderFactory.createMatteBorder(-1, -1, -1, -1, icon); 91 addCompForBorder(border, 92 "matte border (-1,-1,-1,-1,icon)", 93 matteBorders); 94 border = BorderFactory.createMatteBorder(1, 5, 1, 1, Color.red); 95 addCompForBorder(border, 96 "matte border (1,5,1,1,Color.red)", 97 matteBorders); 98 border = BorderFactory.createMatteBorder(0, 20, 0, 0, icon); 99 addCompForBorder(border, 100 "matte border (0,20,0,0,icon)", 101 matteBorders); 102 103 /* ------------------ Third pane: titled borders ------------------ */ 104 this.titledBorders = new JPanel(); 105 this.titledBordersList = new Vector(); 106 this.titledBorders.setBorder(paneEdge); 107 this.titledBorders.setLayout(new BoxLayout(this.titledBorders, 108 BoxLayout.Y_AXIS)); 109 this.titledBorders.add(Box.createRigidArea(new Dimension(0, 10))); 110 JPanel titleFieldPanel = new JPanel(); 111 titleFieldPanel.add(new JLabel("Title : ")); 112 JTextField titlefield = new JTextField(20); 113 titlefield.addActionListener(new ActionListener() { 114 public final void actionPerformed(ActionEvent evt) { 115 String title = ((JTextField) evt.getSource()).getText(); 116 int count = BorderChooser.this.titledBordersList.size(); 117 for (int i = 0; i < count; i++) { 118 Object comp = BorderChooser.this.titledBordersList.get(i); 119 if (comp instanceof TitledBorder) { 120 ((TitledBorder) comp).setTitle(title); 121 } 122 } 123 BorderChooser.this.titledBorders.repaint(); 124 } 125 }); 126 titleFieldPanel.add(titlefield); 127 this.titledBorders.add(titleFieldPanel); 128 TitledBorder titled; 129 130 titled = BorderFactory.createTitledBorder("title"); 131 this.titledBordersList.add(titled); 132 addCompForBorder(titled, 133 "default titled border" 134 + " (default just., default pos.)", 135 this.titledBorders); 136 137 titled = BorderFactory.createTitledBorder(blackline, "title"); 138 this.titledBordersList.add(titled); 139 addCompForTitledBorder(titled, 140 "titled line border" 141 + " (centered, default pos.)", 142 TitledBorder.CENTER, 143 TitledBorder.DEFAULT_POSITION, 144 this.titledBorders); 145 146 titled = BorderFactory.createTitledBorder(etched, "title"); 147 this.titledBordersList.add(titled); 148 addCompForTitledBorder(titled, 149 "titled etched border" 150 + " (right just., default pos.)", 151 TitledBorder.RIGHT, 152 TitledBorder.DEFAULT_POSITION, 153 this.titledBorders); 154 155 titled = BorderFactory.createTitledBorder(loweredbevel, "title"); 156 this.titledBordersList.add(titled); 157 addCompForTitledBorder(titled, 158 "titled lowered bevel border" 159 + " (default just., above top)", 160 TitledBorder.DEFAULT_JUSTIFICATION, 161 TitledBorder.ABOVE_TOP, 162 this.titledBorders); 163 164 titled = BorderFactory.createTitledBorder(empty, "title"); 165 this.titledBordersList.add(titled); 166 addCompForTitledBorder(titled, "titled empty border" 167 + " (default just., bottom)", 168 TitledBorder.DEFAULT_JUSTIFICATION, 169 TitledBorder.BOTTOM, 170 this.titledBorders); 171 172 /* ---------------- Fourth pane: compound borders ----------------- */ 173 JPanel compoundBorders = new JPanel(); 174 compoundBorders.setBorder(paneEdge); 175 compoundBorders.setLayout(new BoxLayout(compoundBorders, 176 BoxLayout.Y_AXIS)); 177 Border redline = BorderFactory.createLineBorder(Color.red); 178 179 Border compound; 180 compound = BorderFactory.createCompoundBorder( 181 raisedbevel, loweredbevel); 182 addCompForBorder(compound, "compound border (two bevels)", 183 compoundBorders); 184 185 compound = BorderFactory.createCompoundBorder( 186 redline, compound); 187 addCompForBorder(compound, "compound border (add a red outline)", 188 compoundBorders); 189 190 titled = BorderFactory.createTitledBorder( 191 compound, "title", 192 TitledBorder.CENTER, 193 TitledBorder.BELOW_BOTTOM); 194 addCompForBorder(titled, 195 "titled compound border" 196 + " (centered, below bottom)", 197 compoundBorders); 198 199 addTab("Simple", null, simpleBorders, null); 200 // addTab("Matte", null, matteBorders, null); 201 addTab("Titled", null, this.titledBorders, null); 202 addTab("Compound", null, compoundBorders, null); 203 setSelectedIndex(0); 204 } 205 206 void addCompForTitledBorder(TitledBorder border, 207 String description, 208 int justification, 209 int position, 210 Container container) { 211 border.setTitleJustification(justification); 212 border.setTitlePosition(position); 213 addCompForBorder(border, description, 214 container); 215 } 216 217 void addCompForBorder(Border border, 218 String description, 219 Container container) { 220 JPanel comp = new JPanel(false); 221 JLabel label = new JLabel(description, JLabel.CENTER); 222 label.setPreferredSize(new Dimension(180,20)); 223 label.setMaximumSize(new Dimension(180,20)); 224 label.setMinimumSize(new Dimension(180,20)); 225 // label.setToolTipText("mouse click selects Border - double click for more..."); 226 comp.setLayout(new GridLayout(1, 1)); 227 comp.add(label); 228 comp.setBorder(border); 229 comp.addMouseListener(new ActiveBorderListener(border,comp)); 230 container.add(Box.createRigidArea(new Dimension(0, 10))); 231 container.add(comp); 232 } 233 234 Border getSelectedBorder(){ 235 return this.border; 236 } 237 238 void setSelectedBorder(Border b, JPanel p){ 239 if (this.border != b){ 240 if (sPrevious != null) 241 sPrevious.setBackground(sColor); 242 sPrevious = p; 243 sColor = p.getBackground(); 244 p.setBackground(UIManager.getColor("Button.select")); 245 p.revalidate(); 246 p.repaint(); 247 } 248 this.border = b; 249 } 250 251 252 final class ActiveBorderListener extends MouseAdapter { 253 Border b; 254 JPanel p; 255 256 public ActiveBorderListener(Border b, JPanel p){ 257 super(); 258 this.b = b; 259 this.p = p; 260 } 261 262 public final void mousePressed(MouseEvent e){ 263 setSelectedBorder(b,p); 264 if (e.getClickCount() == 2){ 265 // bring up title/color editor 266 if (b instanceof TitledBorder){ 267 TitledBorder t = (TitledBorder)b; 268 if (t.getTitle() != null){ 269 String title = JOptionPane.showInputDialog(p, "Please Enter Title String: "); 270 if (title != null){ 271 t.setTitle(title); 272 } 273 } 274 } 275 } 276 } 277 }; 278 279 280 281 public final static void main(String[] args) { 282 JFrame frame = new JFrame("BorderChooser"); 283 284 frame.addWindowListener(new WindowAdapter() { 285 public final void windowClosing(WindowEvent e) { 286 System.exit(0); 287 } 288 }); 289 290 frame.getContentPane().add("Center", new BorderChooser()); 291 frame.pack(); 292 frame.show(); 293 } 294 }

This page was automatically generated by Maven