001: package org.acm.seguin.pmd.swingui;
002:
003: import javax.swing.JButton;
004: import javax.swing.JPanel;
005: import javax.swing.JScrollPane;
006: import javax.swing.JSplitPane;
007: import javax.swing.JTextArea;
008: import javax.swing.UIManager;
009: import javax.swing.border.BevelBorder;
010: import javax.swing.border.CompoundBorder;
011: import javax.swing.border.EmptyBorder;
012: import javax.swing.border.EtchedBorder;
013: import javax.swing.border.LineBorder;
014: import javax.swing.border.TitledBorder;
015: import java.awt.Color;
016: import java.awt.Component;
017: import java.awt.Dimension;
018: import java.awt.FlowLayout;
019: import java.awt.Toolkit;
020: import java.awt.event.ActionListener;
021:
022: /**
023: *
024: * @author Donald A. Leckie
025: * @since August 29, 2002
026: * @version $Revision: 1.1 $, $Date: 2003/07/29 20:51:59 $
027: */
028: class ComponentFactory {
029:
030: /**
031: ******************************************************************************
032: *
033: * @return
034: */
035: protected static final JPanel createButtonPanel() {
036: JPanel buttonPanel = new JPanel(new FlowLayout(
037: FlowLayout.CENTER));
038: EmptyBorder emptyBorder = new EmptyBorder(3, 3, 3, 3);
039: EtchedBorder etchedBorder = new EtchedBorder(
040: EtchedBorder.RAISED);
041: CompoundBorder compoundBorder = new CompoundBorder(
042: etchedBorder, emptyBorder);
043: buttonPanel.setBorder(compoundBorder);
044:
045: return buttonPanel;
046: }
047:
048: /**
049: ******************************************************************************
050: *
051: * @param title
052: *
053: * @return
054: */
055: protected static final JButton createButton(String title) {
056: return createButton(title, null, null);
057: }
058:
059: /**
060: ******************************************************************************
061: *
062: * @param title
063: *
064: * @return
065: */
066: protected static final JButton createButton(String title,
067: Color background, Color foreground) {
068: JButton button;
069: BevelBorder bevelBorder;
070: EtchedBorder etchedBorder;
071: CompoundBorder compoundBorder;
072: LineBorder lineBorder;
073: Dimension size;
074:
075: if (background == null) {
076: background = UIManager.getColor("standardButtonBackground");
077: }
078:
079: if (foreground == null) {
080: foreground = UIManager.getColor("standardButtonForeground");
081: }
082:
083: button = new JButton(title);
084: lineBorder = new LineBorder(Color.DARK_GRAY, 1, true);
085: bevelBorder = new BevelBorder(BevelBorder.RAISED);
086: compoundBorder = new CompoundBorder(bevelBorder, lineBorder);
087: etchedBorder = new EtchedBorder(EtchedBorder.LOWERED);
088: compoundBorder = new CompoundBorder(etchedBorder,
089: compoundBorder);
090: compoundBorder = new CompoundBorder(lineBorder, compoundBorder);
091: size = new Dimension(80, 30);
092:
093: button.setBackground(background);
094: button.setForeground(foreground);
095: button.setBorder(compoundBorder);
096: button.setFont(UIManager.getFont("buttonFont"));
097: button.setSize(size);
098: button.setPreferredSize(size);
099: button.setOpaque(true);
100:
101: return button;
102: }
103:
104: /**
105: *******************************************************************************
106: *
107: */
108: protected static final JButton createSaveButton(
109: ActionListener actionListener) {
110: Color background = UIManager.getColor("pmdGreen");
111: Color foreground = Color.white;
112: JButton saveButton = ComponentFactory.createButton("Save",
113: background, foreground);
114: saveButton.addActionListener(actionListener);
115:
116: return saveButton;
117: }
118:
119: /**
120: *******************************************************************************
121: *
122: */
123: protected static final JButton createCancelButton(
124: ActionListener actionListener) {
125: Color background = UIManager.getColor("pmdRed");
126: Color foreground = Color.white;
127: JButton cancelButton = ComponentFactory.createButton("Cancel",
128: background, foreground);
129: cancelButton.addActionListener(actionListener);
130:
131: return cancelButton;
132: }
133:
134: /**
135: *******************************************************************************
136: *
137: */
138: protected static final JPanel createSaveCancelButtonPanel(
139: ActionListener saveActionListener,
140: ActionListener cancelActionListener) {
141: JPanel buttonPanel = createButtonPanel();
142: buttonPanel.add(createSaveButton(saveActionListener));
143: buttonPanel.add(createCancelButton(cancelActionListener));
144:
145: return buttonPanel;
146: }
147:
148: /**
149: ******************************************************************************
150: *
151: * @param title
152: *
153: * @return
154: */
155: protected static final TitledBorder createTitledBorder(String title) {
156: TitledBorder titledBorder;
157: EtchedBorder etchedBorder;
158: CompoundBorder compoundBorder;
159:
160: etchedBorder = new EtchedBorder(EtchedBorder.LOWERED);
161: compoundBorder = new CompoundBorder(etchedBorder, etchedBorder);
162: titledBorder = new TitledBorder(compoundBorder, title);
163:
164: titledBorder.setTitleFont(UIManager.getFont("titleFont"));
165: titledBorder.setTitleColor(UIManager.getColor("pmdBlue"));
166: titledBorder.setTitleJustification(TitledBorder.LEFT);
167:
168: return titledBorder;
169: }
170:
171: /**
172: ******************************************************************************
173: *
174: * @param view
175: *
176: * @return
177: */
178: protected static final JSplitPane createHorizontalSplitPane(
179: Component leftPane, Component rightPane) {
180: JSplitPane splitPane = new JSplitPane();
181:
182: splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
183: splitPane.setResizeWeight(0.5);
184: splitPane.setDividerSize(10);
185: splitPane.setLeftComponent(leftPane);
186: splitPane.setRightComponent(rightPane);
187: splitPane.setOpaque(true);
188:
189: return splitPane;
190: }
191:
192: /**
193: ******************************************************************************
194: *
195: * @param view
196: *
197: * @return
198: */
199: protected static final JSplitPane createVerticalSplitPane(
200: Component topPane, Component bottomPane) {
201: JSplitPane splitPane = new JSplitPane();
202:
203: splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
204: splitPane.setResizeWeight(0.5);
205: splitPane.setDividerSize(10);
206: splitPane.setTopComponent(topPane);
207: splitPane.setBottomComponent(bottomPane);
208: splitPane.setOpaque(true);
209:
210: return splitPane;
211: }
212:
213: /**
214: ******************************************************************************
215: *
216: * @param view
217: *
218: * @return
219: */
220: protected static final JScrollPane createScrollPane(Component view) {
221: JScrollPane scrollPane = new JScrollPane(view);
222:
223: scrollPane
224: .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
225: scrollPane
226: .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
227: scrollPane.getViewport().setBackground(Color.white);
228: scrollPane.setAutoscrolls(true);
229: scrollPane.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
230:
231: return scrollPane;
232: }
233:
234: /**
235: ******************************************************************************
236: *
237: * @return
238: */
239: protected static final JTextArea createTextArea(String text) {
240: JTextArea textArea;
241: BevelBorder bevelBorder;
242: EmptyBorder emptyBorder;
243: CompoundBorder compoundBorder;
244:
245: textArea = new JTextArea(text);
246: bevelBorder = new BevelBorder(BevelBorder.LOWERED);
247: emptyBorder = new EmptyBorder(3, 3, 3, 3);
248: compoundBorder = new CompoundBorder(bevelBorder, emptyBorder);
249:
250: textArea.setFont(UIManager.getFont("dataFont"));
251: textArea.setBackground(Color.white);
252: textArea.setLineWrap(true);
253: textArea.setWrapStyleWord(true);
254: textArea.setBorder(compoundBorder);
255: textArea.setOpaque(true);
256:
257: return textArea;
258: }
259:
260: /**
261: *******************************************************************************
262: *
263: * @param windowWidth
264: * @param windowHeight
265: */
266: protected static final Dimension adjustWindowSize(int windowWidth,
267: int windowHeight) {
268: Dimension screenSize = Toolkit.getDefaultToolkit()
269: .getScreenSize();
270:
271: if (windowWidth >= screenSize.width) {
272: windowWidth = screenSize.width - 10;
273: }
274:
275: if (windowHeight >= screenSize.height) {
276: windowHeight = screenSize.height - 20;
277: }
278:
279: return new Dimension(windowWidth, windowHeight);
280: }
281: }
|