001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.him.configuration;
020:
021: import java.awt.*;
022: import java.awt.event.*;
023: import java.util.*;
024:
025: import javax.swing.*;
026:
027: import org.openharmonise.him.configuration.lnf.*;
028: import org.openharmonise.him.configuration.reporting.*;
029: import org.openharmonise.him.configuration.searchin.*;
030: import org.openharmonise.him.configuration.sync.*;
031: import org.openharmonise.vfs.context.*;
032: import org.openharmonise.vfs.gui.*;
033:
034: /**
035: * Dialog to present configuration options to the user.
036: *
037: * @author Matthew Large
038: * @version $Revision: 1.1 $
039: *
040: */
041: public class ConfigDialog extends JDialog implements LayoutManager,
042: ActionListener, ContextListener {
043:
044: /**
045: * List of {@link ApplyChangesListener} objects.
046: */
047: private ArrayList m_listeners = new ArrayList();
048:
049: /**
050: * Tabs.
051: */
052: private JTabbedPane m_tabs = null;
053:
054: /**
055: * OK button.
056: */
057: private JButton m_buttonOK = null;
058:
059: /**
060: * Cancel button.
061: */
062: private JButton m_buttonCancel = null;
063:
064: /**
065: * Apply button.
066: */
067: private JButton m_buttonApply = null;
068:
069: /**
070: * Name of tab to be selected when the dialog is first displayed.
071: */
072: private String m_sSelectTab = null;
073:
074: /**
075: * Constructs a new configuration dialog.
076: *
077: * @param frame Owning frame
078: * @param sTitle Title for dialog
079: * @throws HeadlessException
080: */
081: public ConfigDialog(Frame frame, String sTitle)
082: throws HeadlessException {
083: super (frame, sTitle, true);
084: this .setup();
085: }
086:
087: /**
088: * Configures this dialog.
089: *
090: */
091: private void setup() {
092:
093: ContextHandler.getInstance().addListener(
094: ContextType.CONTEXT_APP_FOCUS, this );
095:
096: this .setResizable(false);
097:
098: this .getContentPane().setLayout(this );
099:
100: this .setSize(400, 400);
101: int x = this .getGraphicsConfiguration().getBounds().width / 2
102: - this .getSize().width / 2;
103: int y = this .getGraphicsConfiguration().getBounds().height / 2
104: - this .getSize().height / 2;
105:
106: this .setLocation(x, y);
107:
108: String fontName = "Dialog";
109: int fontSize = 11;
110: Font font = new Font(fontName, Font.PLAIN, fontSize);
111: this .getContentPane().setBackground(new Color(224, 224, 224));
112:
113: this .m_buttonOK = new JButton("OK");
114: this .m_buttonOK.setActionCommand("OK");
115: this .m_buttonOK.addActionListener(this );
116: this .m_buttonOK.setFont(font);
117: this .getContentPane().add(this .m_buttonOK);
118:
119: this .m_buttonCancel = new JButton("Cancel");
120: this .m_buttonCancel.setActionCommand("CANCEL");
121: this .m_buttonCancel.addActionListener(this );
122: this .m_buttonCancel.setFont(font);
123: this .getContentPane().add(this .m_buttonCancel);
124:
125: this .m_buttonApply = new JButton("Apply");
126: this .m_buttonApply.setEnabled(false);
127: this .m_buttonApply.setActionCommand("APPLY");
128: this .m_buttonApply.addActionListener(this );
129: this .m_buttonApply.setFont(font);
130: this .getContentPane().add(this .m_buttonApply);
131:
132: this .m_tabs = new JTabbedPane();
133: this .m_tabs.setFont(font);
134:
135: JPanel panel = new JPanel();
136: panel.setLayout(new FlowLayout(FlowLayout.CENTER));
137:
138: ReportingConfigOptions reportingOptions = new ReportingConfigOptions(
139: this );
140: panel.add(reportingOptions);
141:
142: LookAndFeelConfigOptions options = new LookAndFeelConfigOptions(
143: this );
144: panel.add(options);
145:
146: SyncConfigOptions syncOptions = new SyncConfigOptions(this );
147: panel.add(syncOptions);
148:
149: this .m_tabs.addTab("Options", panel);
150:
151: panel = new SearchInOptions(this );
152:
153: this .m_tabs.addTab("Search in", panel);
154:
155: this .getContentPane().add(this .m_tabs);
156:
157: }
158:
159: /**
160: * Sets the name of the tab to be selected when the dialog is
161: * shown.
162: *
163: * @param sTabName Name of tab
164: */
165: public void setSelectedTab(String sTabName) {
166: this .m_sSelectTab = sTabName;
167: }
168:
169: /* (non-Javadoc)
170: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
171: */
172: public void actionPerformed(ActionEvent ae) {
173: if (ae.getActionCommand().equals("OK")) {
174: this .fireApplyChanges();
175: ContextHandler.getInstance().removeListener(
176: ContextType.CONTEXT_APP_FOCUS, this );
177: this .hide();
178: } else if (ae.getActionCommand().equals("CANCEL")) {
179: ContextHandler.getInstance().removeListener(
180: ContextType.CONTEXT_APP_FOCUS, this );
181: this .hide();
182: } else if (ae.getActionCommand().equals("APPLY")) {
183: this .m_buttonApply.setEnabled(false);
184: this .fireApplyChanges();
185:
186: }
187: }
188:
189: /* (non-Javadoc)
190: * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
191: */
192: public void layoutContainer(Container arg0) {
193: this .m_tabs.setSize(this .getSize().width - 10, 430);
194: this .m_tabs.setLocation(0, 0);
195:
196: this .m_buttonOK.setSize(70, 20);
197: this .m_buttonOK.setLocation(150, 340);
198:
199: this .m_buttonCancel.setSize(70, 20);
200: this .m_buttonCancel.setLocation(230, 340);
201:
202: this .m_buttonApply.setSize(70, 20);
203: this .m_buttonApply.setLocation(310, 340);
204: }
205:
206: /**
207: * Adds an apply changes listener.
208: *
209: * @param listener Listener to add
210: */
211: public void addApplyChangesListener(ApplyChangesListener listener) {
212: this .m_listeners.add(listener);
213: }
214:
215: /**
216: * Removes an apply changes listener.
217: *
218: * @param listener Listener to remove
219: */
220: public void removeApplyChangesListener(ApplyChangesListener listener) {
221: this .m_listeners.remove(listener);
222: }
223:
224: /**
225: * Fires an apply changes message to all listener.
226: *
227: */
228: private void fireApplyChanges() {
229: Iterator itor = this .m_listeners.iterator();
230: while (itor.hasNext()) {
231: ApplyChangesListener listener = (ApplyChangesListener) itor
232: .next();
233: listener.applyChanges();
234: }
235: }
236:
237: /**
238: * Called by an options component to inform the dialog that a change
239: * has been made to a configuration option. This will enable the apply
240: * button.
241: *
242: */
243: public void changesMade() {
244: this .m_buttonApply.setEnabled(true);
245: }
246:
247: public static void main(String[] args) {
248: JFrame frame = new JFrame();
249: frame.setIconImage(((ImageIcon) IconManager.getInstance()
250: .getIcon("32-sim-logo.gif")).getImage());
251:
252: ConfigDialog dialog = new ConfigDialog(frame, "Customise");
253: dialog.show();
254: System.exit(0);
255: }
256:
257: /**
258: * @param arg0
259: * @throws java.awt.HeadlessException
260: */
261: private ConfigDialog(Dialog arg0) throws HeadlessException {
262: super (arg0);
263: }
264:
265: /**
266: * @param arg0
267: * @param arg1
268: * @throws java.awt.HeadlessException
269: */
270: private ConfigDialog(Dialog arg0, boolean arg1)
271: throws HeadlessException {
272: super (arg0, arg1);
273: }
274:
275: /**
276: * @param arg0
277: * @throws java.awt.HeadlessException
278: */
279: private ConfigDialog(Frame arg0) throws HeadlessException {
280: super (arg0);
281: }
282:
283: /**
284: * @param arg0
285: * @param arg1
286: * @throws java.awt.HeadlessException
287: */
288: private ConfigDialog(Frame arg0, boolean arg1)
289: throws HeadlessException {
290: super (arg0, arg1);
291: }
292:
293: /**
294: * @param arg0
295: * @param arg1
296: * @throws java.awt.HeadlessException
297: */
298: private ConfigDialog() throws HeadlessException {
299: super ();
300: }
301:
302: /**
303: * @param arg0
304: * @param arg1
305: * @param arg2
306: * @throws java.awt.HeadlessException
307: */
308: private ConfigDialog(Dialog arg0, String arg1, boolean arg2)
309: throws HeadlessException {
310: super (arg0, arg1, arg2);
311: }
312:
313: /**
314: * @param arg0
315: * @param arg1
316: * @throws java.awt.HeadlessException
317: */
318: private ConfigDialog(Dialog arg0, String arg1)
319: throws HeadlessException {
320: super (arg0, arg1);
321: }
322:
323: /**
324: * @param arg0
325: * @param arg1
326: * @param arg2
327: * @throws java.awt.HeadlessException
328: */
329: private ConfigDialog(Frame arg0, String arg1, boolean arg2)
330: throws HeadlessException {
331: super (arg0, arg1, arg2);
332: }
333:
334: /**
335: * @param arg0
336: * @param arg1
337: * @param arg2
338: * @param arg3
339: * @throws java.awt.HeadlessException
340: */
341: private ConfigDialog(Dialog arg0, String arg1, boolean arg2,
342: GraphicsConfiguration arg3) throws HeadlessException {
343: super (arg0, arg1, arg2, arg3);
344: }
345:
346: /**
347: * @param arg0
348: * @param arg1
349: * @param arg2
350: * @param arg3
351: */
352: private ConfigDialog(Frame arg0, String arg1, boolean arg2,
353: GraphicsConfiguration arg3) {
354: super (arg0, arg1, arg2, arg3);
355: }
356:
357: /* (non-Javadoc)
358: * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
359: */
360: public void removeLayoutComponent(Component arg0) {
361: }
362:
363: /* (non-Javadoc)
364: * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
365: */
366: public void addLayoutComponent(String arg0, Component arg1) {
367: }
368:
369: /* (non-Javadoc)
370: * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
371: */
372: public Dimension minimumLayoutSize(Container arg0) {
373: return this .getSize();
374: }
375:
376: /* (non-Javadoc)
377: * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
378: */
379: public Dimension preferredLayoutSize(Container arg0) {
380: return this .getSize();
381: }
382:
383: /* (non-Javadoc)
384: * @see java.awt.Component#show()
385: */
386: public void show() {
387: if (this .m_sSelectTab != null) {
388: int nIndex = 0;
389: for (int i = 0; i < this .m_tabs.getTabCount(); i++) {
390: if (this .m_tabs.getTitleAt(i).equals(this .m_sSelectTab)) {
391: nIndex = i;
392: }
393: }
394: this .m_tabs.setSelectedIndex(nIndex);
395: }
396: super .show();
397: }
398:
399: /* (non-Javadoc)
400: * @see com.simulacramedia.contentmanager.context.ContextListener#contextMessage(com.simulacramedia.contentmanager.context.ContextEvent)
401: */
402: public void contextMessage(ContextEvent ce) {
403: if (ce.CONTEXT_TYPE == ContextType.CONTEXT_APP_FOCUS) {
404: this.toFront();
405: }
406: }
407:
408: }
|