View Javadoc

1   package org.catacomb.druid.swing;
2   
3   import org.catacomb.interlish.interact.DComponent;
4   import org.catacomb.interlish.structure.MouseActor;
5   import org.catacomb.interlish.structure.MouseSource;
6   import org.catacomb.report.E;
7   import org.catacomb.util.ColorUtil;
8   
9   
10  import java.awt.*;
11  
12  import javax.swing.BorderFactory;
13  import javax.swing.JComponent;
14  import javax.swing.JPanel;
15  import javax.swing.border.BevelBorder;
16  import javax.swing.border.Border;
17  import javax.swing.border.CompoundBorder;
18  import javax.swing.border.TitledBorder;
19  
20  
21  public class DPanel extends JPanel implements DComponent, MouseSource {
22  
23      static final long serialVersionUID = 1001;
24  
25      private Border lineborder;
26  
27      private Font borderFont;
28      private Color borderColor;
29  
30      public final static int PLAINFONT = 1;
31      public final static int BOLDFONT = 2;
32  
33  
34      public void setMouseActor(MouseActor ma) {
35          addMouseListener(new DMouseRelay(ma));
36      }
37  
38  
39      public void setTooltip(String s) {
40          setToolTipText(s);
41      }
42  
43      public void setBg(Color c) {
44          setBackground(c);
45          if (c instanceof javax.swing.plaf.ColorUIResource) {
46              E.warning("default UI bg color being applied - should be overridden: ");
47              (new Exception()).printStackTrace();
48          }
49      }
50  
51  
52      public void setFg(Color c) {
53          setForeground(c);
54          if (c instanceof javax.swing.plaf.ColorUIResource) {
55              E.warning("default UI fg color being applied - should be overridden: ");
56              (new Exception()).printStackTrace();
57          }
58      }
59  
60  
61  
62      public int[] getCenterLocationOnScreen() {
63          Point p = getLocationOnScreen();
64          Dimension d = getSize();
65          int[] xy = new int[2];
66          xy[0] = p.x + d.width / 2;
67          xy[1] = p.y + d.height / 2;
68          return xy;
69      }
70  
71  
72      public int[] getXYLocation() {
73          return getCenterLocationOnScreen();
74      }
75  
76      public void localSetBorder(Border b) {
77          if (getBorder() instanceof TitledBorder) {
78              E.error("replacing titled border with " + b);
79          }
80          super.setBorder(b);
81      }
82  
83  
84      public void setEtchedBorder(Color col) {
85          localSetBorder(BorderUtil.makeEtchedBorder(col));
86      }
87  
88  
89      public void setEtchedUpBorder(Color col) {
90          localSetBorder(BorderUtil.makeEtchedUpBorder(col));
91      }
92  
93  
94      public void addEtchedBorder(Color col) {
95          Border bb = BorderUtil.makeEtchedBorder(col);
96          addBorder(bb);
97      }
98  
99      public void addSunkenBorder(Color col) {
100 
101         Color cbr = ColorUtil.brighter(col);
102         Color cdk = ColorUtil.slightlyDarker(col);
103 
104         Border bb = BorderFactory.createBevelBorder(BevelBorder.LOWERED, col, cbr, cdk, col);
105         addBorder(bb);
106     }
107 
108 
109     public void clearBorder() {
110         if (getBorder() != null) {
111             setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
112         }
113     }
114 
115 
116     public void addBorder(int l, int r, int t, int b) {
117         if (l == 0 && r == 0 && t == 0 && b == 0) {
118             E.warning("DPanel add border called with zero width " + this);
119         } else {
120             Border border = BorderFactory.createEmptyBorder(t, l, b, r);
121             addBorder(border);
122         }
123     }
124 
125     public void setEmptyBorder(int l, int r, int t, int b) {
126         Border border = BorderFactory.createEmptyBorder(t, l, b, r);
127         localSetBorder(border);
128 
129     }
130 
131 
132 
133 
134     private void addBorder(Border bb) {
135         Border bsf = getBorder();
136         if (bsf == null) {
137             localSetBorder(bb);
138         } else {
139             CompoundBorder cbd = new CompoundBorder(bb, bsf);
140             localSetBorder(cbd);
141         }
142     }
143 
144 
145 
146 
147 
148     public void setSunkenBorder(Color col) {
149         Color cbr = col.brighter();
150         Color cdk = col.darker();
151 
152         localSetBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED, col, cbr, cdk, col));
153     }
154 
155 
156 
157     public void setFlowLayout() {
158         setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
159     }
160 
161 
162     public void setSingle() {
163         setLayout(new GridLayout(1, 1, 0, 0));
164     }
165 
166 
167     public void addSingle(Object obj) {
168         setSingle();
169         add((JComponent)obj);
170     }
171 
172 
173     public void setFlowLeft(int h, int v) {
174         setLayout(new FlowLayout(FlowLayout.LEFT, h, v));
175     }
176 
177 
178     public void setFlowRight(int h, int v) {
179         setLayout(new FlowLayout(FlowLayout.RIGHT, h, v));
180     }
181 
182     public void setFlowCenter(int h, int v) {
183         setLayout(new FlowLayout(FlowLayout.CENTER, h, v));
184     }
185 
186 
187     public void setBorderLayout() {
188         setLayout(new BorderLayout(0, 0));
189     }
190 
191 
192     public void setBorderLayout(int h, int v) {
193         setLayout(new BorderLayout(h, v));
194     }
195 
196 
197 
198     public void addTitledBorder(String s, Color c) {
199         if (lineborder == null) {
200 //         lineborder = BorderFactory.createLineBorder(new Color(210, 210, 210));
201             lineborder = BorderFactory.createLineBorder(c);
202         }
203 
204         if (borderFont == null) {
205             borderFont = new Font("sansserif", Font.BOLD, 12);
206             borderColor = new Color(90, 90, 90);
207         }
208 
209         localSetBorder(BorderFactory.createTitledBorder(lineborder, s,
210                        TitledBorder.LEADING, TitledBorder.TOP, borderFont, borderColor));
211     }
212 
213 
214     public void addTitledBorder(String s, int ifont, int txtcol, int linecol) {
215         lineborder = BorderFactory.createLineBorder(new Color(linecol));
216 
217         Font f = null;
218         if (ifont == PLAINFONT) {
219             f = new Font("sansserif", Font.PLAIN, 12);
220         } else {
221             f = new Font("sansserif", Font.BOLD, 12);
222         }
223 
224         Color tc = new Color(txtcol);
225         Border b = BorderFactory.createTitledBorder(lineborder, s, TitledBorder.CENTER, TitledBorder.TOP, f, tc);
226         addBorder(b);
227     }
228 
229 
230     public void setPreferredSize(int w, int h) {
231         setPreferredSize(new Dimension(w, h));
232     }
233 
234 
235     public int[] getXYLocationOnScreen() {
236         int[] ixy = {400, 400};
237         if (isShowing()) {
238             Point p = getLocationOnScreen();
239             ixy[0] = (int)(p.getX());
240             ixy[1] = (int)(p.getY());
241         }
242         return ixy;
243     }
244 
245 
246     public void addDComponent(DComponent obj) {
247         add((JComponent)obj);
248     }
249 
250     public void addDComponent(DComponent obj, Object constraints) {
251         add((JComponent)obj, constraints);
252     }
253 
254 
255     public void removeDComponent(DComponent obj) {
256         remove((JComponent)obj);
257     }
258 
259 
260     public void setGridLayout(int nr, int nc, int dx, int dy) {
261         setLayout(new GridLayout(nr, nc, dx, dy));
262     }
263 
264 }