001: /*
002: * JSOptionDialog.java - options dialog for JavaStyle option panes
003: * Copyright (C) 2001 Dirk Moebius
004: *
005: * jEdit buffer options:
006: * :tabSize=4:indentSize=4:noTabs=false:maxLineLen=0:
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022: package org.acm.seguin.ide.common.options;
023:
024: import java.awt.BorderLayout;
025: import java.awt.event.ActionEvent;
026: import java.awt.event.ActionListener;
027:
028: import javax.swing.Box;
029: import javax.swing.BoxLayout;
030: import javax.swing.JButton;
031: import javax.swing.JPanel;
032: import javax.swing.JTabbedPane;
033: import javax.swing.JDialog;
034: import javax.swing.border.EmptyBorder;
035: import javax.swing.JComboBox;
036: import java.awt.event.ContainerAdapter;
037: import java.awt.event.ContainerEvent;
038: import java.awt.event.KeyAdapter;
039: import java.awt.event.KeyEvent;
040: import java.awt.event.WindowAdapter;
041: import java.awt.event.WindowEvent;
042: import java.awt.Component;
043: import java.awt.Container;
044: import java.awt.Frame;
045:
046: import org.acm.seguin.ide.common.IDEPlugin;
047:
048: /**
049: * An option dialog for JavaStyle options.
050: *
051: *@author Mike Atkinson (<a href="mailto:javastyle@ladyshot.demon.co.uk">
052: * Mike@ladyshot.demon.co.uk</a> )
053: *@author Dirk Moebius (<a href="mailto:dmoebius@gmx.net">dmoebius@gmx.net
054: * </a>)
055: *@created 04 September 2003
056: *@version $Version: $
057: *@since 1.0
058: */
059: public class JSOptionDialog extends JDialog implements ActionListener {
060: private JButton apply;
061: private JButton cancel;
062:
063: private JButton ok;
064: private JSHelpOptionPane[][] optionPanes;
065: private String[] projects = new String[] { "default" };
066:
067: //{{{ Private members
068:
069: private void _init() {
070: ((Container) getLayeredPane())
071: .addContainerListener(new ContainerHandler());
072: getContentPane().addContainerListener(new ContainerHandler());
073:
074: keyHandler = new KeyHandler();
075: addKeyListener(keyHandler);
076: addWindowListener(new WindowHandler());
077:
078: setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
079: }
080:
081: //}}}
082:
083: // protected members
084: protected KeyHandler keyHandler;
085:
086: // Recursively adds our key listener to sub-components
087: class ContainerHandler extends ContainerAdapter {
088: public void componentAdded(ContainerEvent evt) {
089: componentAdded(evt.getChild());
090: }
091:
092: public void componentRemoved(ContainerEvent evt) {
093: componentRemoved(evt.getChild());
094: }
095:
096: private void componentAdded(Component comp) {
097: comp.addKeyListener(keyHandler);
098: if (comp instanceof Container) {
099: Container cont = (Container) comp;
100: cont.addContainerListener(this );
101: Component[] comps = cont.getComponents();
102: for (int i = 0; i < comps.length; i++) {
103: componentAdded(comps[i]);
104: }
105: }
106: }
107:
108: private void componentRemoved(Component comp) {
109: comp.removeKeyListener(keyHandler);
110: if (comp instanceof Container) {
111: Container cont = (Container) comp;
112: cont.removeContainerListener(this );
113: Component[] comps = cont.getComponents();
114: for (int i = 0; i < comps.length; i++) {
115: componentRemoved(comps[i]);
116: }
117: }
118: }
119: }
120:
121: class KeyHandler extends KeyAdapter {
122: public void keyPressed(KeyEvent evt) {
123: if (evt.isConsumed())
124: return;
125:
126: if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
127: // crusty workaround
128: Component comp = getFocusOwner();
129: while (comp != null) {
130: if (comp instanceof JComboBox) {
131: JComboBox combo = (JComboBox) comp;
132: if (combo.isEditable()) {
133: Object selected = combo.getEditor()
134: .getItem();
135: if (selected != null)
136: combo.setSelectedItem(selected);
137: }
138: break;
139: }
140:
141: comp = comp.getParent();
142: }
143:
144: ok();
145: evt.consume();
146: } else if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
147: cancel();
148: evt.consume();
149: }
150: }
151: }
152:
153: class WindowHandler extends WindowAdapter {
154: public void windowClosing(WindowEvent evt) {
155: cancel();
156: }
157: }
158:
159: /**
160: * Constructor for the JSOptionDialog object
161: *
162: *@param view Description of Parameter
163: */
164: public JSOptionDialog(Frame parent) {
165: super (parent, IDEPlugin.getProperty("options.javastyle.label"),
166: true);
167: _init();
168: projects = IDEPlugin.getProjects(parent);
169:
170: IDEPlugin.showWaitCursor(parent);
171:
172: JPanel content = new JPanel(new BorderLayout());
173: content.setBorder(new EmptyBorder(5, 8, 8, 8));
174: content.setLayout(new BorderLayout());
175: setContentPane(content);
176:
177: optionPanes = new JSHelpOptionPane[projects.length][];
178:
179: JTabbedPane mainstage = new JTabbedPane(
180: ((projects.length <= 1) ? JTabbedPane.LEFT
181: : JTabbedPane.TOP));
182: for (int proj = 0; proj < projects.length; proj++) {
183: optionPanes[proj] = new JSHelpOptionPane[] {
184: new JSGeneralOptionPane(projects[proj]),
185: new JSIndentOptionPane(projects[proj]),
186: new JSSpacingOptionPane(projects[proj]),
187: new JSAlignmentOptionPane(projects[proj]),
188: new JSSortOptionPane(projects[proj]),
189: new JSJavadocOptionPane(projects[proj]),
190: new JSStubsOptionPane(projects[proj]),
191: new JSStubs2OptionPane(projects[proj]),
192: new JSStubsJUnitOptionPane(projects[proj]),
193: new JSTagsOptionPane(projects[proj]),
194: new JSCommentOptionPane(projects[proj]),
195: new PMDOptionPane(projects[proj]),
196: new NavigatorOptionPane(projects[proj]), };
197:
198: JTabbedPane stage = new JTabbedPane(JTabbedPane.LEFT);
199: for (int i = 0; i < optionPanes[proj].length; ++i) {
200: optionPanes[proj][i].setBorder(new EmptyBorder(12, 12,
201: 12, 12));
202: optionPanes[proj][i].init();
203: if (projects.length <= 1) {
204: mainstage.addTab(IDEPlugin
205: .getProperty("options."
206: + optionPanes[proj][i].getName()
207: + ".label"), optionPanes[proj][i]);
208: } else {
209: stage.addTab(IDEPlugin
210: .getProperty("options."
211: + optionPanes[proj][i].getName()
212: + ".label"), optionPanes[proj][i]);
213: }
214: }
215: if (projects.length > 1) {
216: mainstage.addTab(projects[proj], stage);
217: }
218: }
219: content.add(mainstage, BorderLayout.CENTER);
220:
221: JPanel buttons = new JPanel();
222: buttons.setBorder(new EmptyBorder(12, 0, 0, 0));
223: buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
224: buttons.add(Box.createGlue());
225:
226: ok = new JButton(IDEPlugin.getProperty("common.ok"));
227: ok.addActionListener(this );
228: buttons.add(ok);
229: buttons.add(Box.createHorizontalStrut(6));
230: getRootPane().setDefaultButton(ok);
231:
232: cancel = new JButton(IDEPlugin.getProperty("common.cancel"));
233: cancel.addActionListener(this );
234: buttons.add(cancel);
235: buttons.add(Box.createHorizontalStrut(6));
236:
237: apply = new JButton(IDEPlugin.getProperty("common.apply"));
238: apply.addActionListener(this );
239: buttons.add(apply);
240:
241: buttons.add(Box.createGlue());
242:
243: content.add(buttons, BorderLayout.SOUTH);
244:
245: IDEPlugin.hideWaitCursor(parent);
246: pack();
247: setLocationRelativeTo(parent);
248: show();
249: }
250:
251: /**
252: * Description of the Method
253: *
254: *@param evt Description of Parameter
255: */
256: public void actionPerformed(ActionEvent evt) {
257: Object source = evt.getSource();
258:
259: if (source == ok) {
260: ok();
261: } else if (source == cancel) {
262: cancel();
263: } else if (source == apply) {
264: ok(false);
265: }
266: }
267:
268: /**
269: * Description of the Method
270: */
271: public void cancel() {
272: dispose();
273: }
274:
275: /**
276: * Description of the Method
277: */
278: public void ok() {
279: ok(true);
280: }
281:
282: /**
283: * Description of the Method
284: *
285: *@param dispose Description of Parameter
286: */
287: public void ok(final boolean dispose) {
288: for (int proj = 0; proj < optionPanes.length; ++proj) {
289: for (int i = 0; i < optionPanes[proj].length; ++i) {
290: optionPanes[proj][i].save();
291: }
292: }
293:
294: IDEPlugin.saveProperties();
295:
296: // get rid of this dialog if necessary
297: if (dispose) {
298: dispose();
299: }
300: }
301:
302: }
|