001: /*
002: The contents of this file are subject to the Common Public Attribution License
003: Version 1.0 (the "License"); you may not use this file except in compliance with
004: the License. You may obtain a copy of the License at
005: http://www.projity.com/license . The License is based on the Mozilla Public
006: License Version 1.1 but Sections 14 and 15 have been added to cover use of
007: software over a computer network and provide for limited attribution for the
008: Original Developer. In addition, Exhibit A has been modified to be consistent
009: with Exhibit B.
010:
011: Software distributed under the License is distributed on an "AS IS" basis,
012: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
013: specific language governing rights and limitations under the License. The
014: Original Code is OpenProj. The Original Developer is the Initial Developer and
015: is Projity, Inc. All portions of the code written by Projity are Copyright (c)
016: 2006, 2007. All Rights Reserved. Contributors Projity, Inc.
017:
018: Alternatively, the contents of this file may be used under the terms of the
019: Projity End-User License Agreeement (the Projity License), in which case the
020: provisions of the Projity License are applicable instead of those above. If you
021: wish to allow use of your version of this file only under the terms of the
022: Projity License and not to allow others to use your version of this file under
023: the CPAL, indicate your decision by deleting the provisions above and replace
024: them with the notice and other provisions required by the Projity License. If
025: you do not delete the provisions above, a recipient may use your version of this
026: file under either the CPAL or the Projity License.
027:
028: [NOTE: The text of this license may differ slightly from the text of the notices
029: in Exhibits A and B of the license at http://www.projity.com/license. You should
030: use the latest text at http://www.projity.com/license for your modifications.
031: You may not remove this license text from the source files.]
032:
033: Attribution Information: Attribution Copyright Notice: Copyright � 2006, 2007
034: Projity, Inc. Attribution Phrase (not exceeding 10 words): Powered by OpenProj,
035: an open source solution from Projity. Attribution URL: http://www.projity.com
036: Graphic Image as provided in the Covered Code as file: openproj_logo.png with
037: alternatives listed on http://www.projity.com/logo
038:
039: Display of Attribution Information is required in Larger Works which are defined
040: in the CPAL as a work which combines Covered Code or portions thereof with code
041: not governed by the terms of the CPAL. However, in addition to the other notice
042: obligations, all copies of the Covered Code in Executable and Source Code form
043: distributed must, as a form of attribution of the original author, include on
044: each user interface screen the "OpenProj" logo visible to all users. The
045: OpenProj logo should be located horizontally aligned with the menu bar and left
046: justified on the top left of the screen adjacent to the File menu. The logo
047: must be at least 100 x 25 pixels. When users click on the "OpenProj" logo it
048: must direct them back to http://www.projity.com.
049: */
050: package com.projity.dialog;
051:
052: import java.awt.BorderLayout;
053: import java.awt.Component;
054: import java.awt.Frame;
055: import java.awt.Point;
056: import java.awt.event.ActionEvent;
057: import java.awt.event.ActionListener;
058: import java.awt.event.KeyEvent;
059: import java.awt.event.MouseAdapter;
060: import java.awt.event.MouseEvent;
061: import java.awt.event.MouseListener;
062:
063: import javax.swing.BoxLayout;
064: import javax.swing.ComboBoxModel;
065: import javax.swing.DefaultComboBoxModel;
066: import javax.swing.ImageIcon;
067: import javax.swing.JButton;
068: import javax.swing.JComponent;
069: import javax.swing.JDialog;
070: import javax.swing.JLabel;
071: import javax.swing.JOptionPane;
072: import javax.swing.JRadioButton;
073: import javax.swing.JRootPane;
074: import javax.swing.JToolTip;
075: import javax.swing.KeyStroke;
076: import javax.swing.plaf.ButtonUI;
077: import javax.swing.plaf.basic.BasicButtonUI;
078:
079: import org.apache.commons.collections.Closure;
080:
081: import com.projity.configuration.FieldDictionary;
082: import com.projity.configuration.Settings;
083: import com.projity.help.HelpUtil;
084: import com.projity.menu.HyperLinkToolTip;
085: import com.projity.pm.graphic.IconManager;
086: import com.projity.pm.graphic.frames.DocumentFrame;
087: import com.projity.pm.graphic.frames.GraphicManager;
088: import com.projity.pm.graphic.model.cache.NodeModelCache;
089: import com.projity.pm.graphic.model.cache.ReferenceNodeModelCache;
090: import com.projity.strings.Messages;
091: import com.projity.util.BrowserControl;
092:
093: /**
094: *
095: */
096: public abstract class AbstractDialog extends JDialog {
097: protected JButton ok;
098:
099: protected JButton cancel;
100: protected JLabel help;
101:
102: protected Frame owner;
103: private int dialogResult = JOptionPane.CANCEL_OPTION;
104:
105: protected JComponent contentPanel = null;
106: protected ButtonPanel buttonPanel = null;
107: private String helpAddress = null;
108:
109: //protected MainFrame main;
110:
111: public AbstractDialog() {
112: super ();
113: }
114:
115: public AbstractDialog(Frame owner/*, MainFrame main*/,
116: String title, boolean modal) {
117: super (owner, title, modal);
118: createRootPane();
119: setLocationRelativeTo(null);
120: this .owner = owner;
121: }
122:
123: // see http://www.javaworld.com/javaworld/javatips/jw-javatip72.html
124: protected JRootPane createRootPane() {
125: ActionListener escapeListener = new ActionListener() {
126: public void actionPerformed(ActionEvent actionEvent) {
127: onCancel();
128:
129: }
130: };
131: ActionListener enterListener = new ActionListener() {
132: public void actionPerformed(ActionEvent actionEvent) {
133: onOk();
134:
135: }
136: };
137:
138: ActionListener helpListener = new ActionListener() {
139: public void actionPerformed(ActionEvent actionEvent) {
140: onHelp();
141:
142: }
143:
144: };
145: JRootPane rootPane = new JRootPane();
146: KeyStroke escapeStroke = KeyStroke.getKeyStroke(
147: KeyEvent.VK_ESCAPE, 0);
148: rootPane.registerKeyboardAction(escapeListener, escapeStroke,
149: JComponent.WHEN_IN_FOCUSED_WINDOW);
150: KeyStroke enterStroke = KeyStroke.getKeyStroke(
151: KeyEvent.VK_ENTER, 0);
152: rootPane.registerKeyboardAction(enterListener, enterStroke,
153: JComponent.WHEN_IN_FOCUSED_WINDOW);
154: KeyStroke f1Stroke = KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0);
155: rootPane.registerKeyboardAction(helpListener, f1Stroke,
156: JComponent.WHEN_IN_FOCUSED_WINDOW);
157: KeyStroke f2Stroke = KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0);
158: rootPane.registerKeyboardAction(helpListener, f2Stroke,
159: JComponent.WHEN_IN_FOCUSED_WINDOW);
160: return rootPane;
161: }
162:
163: protected boolean hasHelp() {
164: return helpAddress != null;
165: }
166:
167: protected void onHelp() {
168: if (helpAddress != null)
169: BrowserControl.displayURL(helpAddress);
170: else
171: System.out.println("no help available");
172: // TODO Auto-generated method stub
173:
174: }
175:
176: /**
177: *
178: */
179: protected void onCancel() {
180: setVisible(false);
181: setDialogResult(JOptionPane.CANCEL_OPTION);
182: // desactivateListeners();
183: }
184:
185: /**
186: *
187: */
188: public void onOk() {
189: if (!bind(false))
190: return;
191: setDialogResult(JOptionPane.OK_OPTION);
192: setVisible(false);
193: // desactivateListeners();
194: }
195:
196: public abstract JComponent createContentPanel();
197:
198: public void setVisible(boolean b) {
199: if (b && !listenersActivated)
200: activateListeners();
201: else if (!b && listenersActivated)
202: desactivateListeners();
203: super .setVisible(b);
204: }
205:
206: protected boolean listenersActivated = true;
207:
208: protected void activateListeners() {
209: listenersActivated = true;
210: }
211:
212: protected void desactivateListeners() {
213: listenersActivated = false;
214: }
215:
216: protected boolean bind(boolean get) {
217: return true;
218: }
219:
220: protected void initComponents() {
221: contentPanel = createContentPanel();
222: buttonPanel = createButtonPanel();
223: getContentPane().setLayout(new BorderLayout());
224: if (contentPanel != null)
225: getContentPane().add(contentPanel, BorderLayout.CENTER);
226: if (buttonPanel != null)
227: getContentPane().add(buttonPanel,
228: BorderLayout.AFTER_LAST_LINE);
229:
230: }
231:
232: public void pack() {
233: initComponents();
234: super .pack();
235: }
236:
237: protected void createOkCancelButtons(String okText,
238: String cancelText) {
239: ok = new JButton(okText);
240: ok.setEnabled(initialOkEnabledState());
241: ok.addActionListener(new ActionListener() {
242: public void actionPerformed(ActionEvent e) {
243: AbstractDialog.this .onOk();
244: }
245: });
246: cancel = new JButton(cancelText);
247: cancel.addActionListener(new ActionListener() {
248: public void actionPerformed(ActionEvent e) {
249: AbstractDialog.this .onCancel();
250: }
251: });
252: // if (hasHelp()) {
253: //
254: // help= new JLabel(IconManager.getIcon("menu24.help"));
255: // help.setToolTipText(Messages.getString("Text.Help")); //$NON-NLS-1$
256: // help.addMouseListener(new MouseAdapter() {
257: // public void mouseClicked(MouseEvent arg0) {
258: // AbstractDialog.this.onHelp();
259: // }});
260: // }
261: }
262:
263: protected void createOkCancelButtons() {
264: createOkCancelButtons(Messages.getString("ButtonText.OK"),
265: Messages.getString("ButtonText.Cancel"));
266: }
267:
268: protected void createCloseButton() {
269: ok = new JButton(Messages.getString("ButtonText.Close"));
270: ok.addActionListener(new ActionListener() {
271: public void actionPerformed(ActionEvent e) {
272: AbstractDialog.this .onOk();
273: }
274: });
275: }
276:
277: public ButtonPanel createButtonPanel() {
278: if (!hasOkAndCancelButtons() && !hasCloseButton())
279: return null;
280: if (hasCloseButton())
281: createCloseButton();
282: else
283: createOkCancelButtons();
284: ButtonPanel buttonPanel = new ButtonPanel();
285: buttonPanel.addButton(ok);
286: if (hasOkAndCancelButtons())
287: buttonPanel.addButton(cancel);
288: // if (hasHelp())
289: // buttonPanel.add(help);
290: return buttonPanel;
291: }
292:
293: public JComponent createBannerPanel() {
294: return null;
295: }
296:
297: public boolean doModal() {
298: pack();
299: setLocationRelativeTo(getParent());// to center on parent
300: setVisible(true);
301: return (getDialogResult() != JOptionPane.CANCEL_OPTION);
302: }
303:
304: public Object getBean() {
305: return null;
306: }
307:
308: public int execute(Closure setter, Closure getter) {
309: pack();
310: setter.execute(getBean());
311: bind(true);
312: setLocationRelativeTo(null);// to center on screen
313: setVisible(true);
314: if (getDialogResult() != JOptionPane.CANCEL_OPTION) {
315: // bind(false); //already done in onOk
316: if (getter != null)
317: getter.execute(getBean());
318: }
319: return getDialogResult();
320: }
321:
322: protected boolean initialOkEnabledState() {
323: return true;
324: }
325:
326: // see com.projity.configuration.configuration.xml for available fieldId
327: public static ComboBoxModel getComboBoxModel(String fieldId) {
328: Object[] options = FieldDictionary.getInstance()
329: .getFieldFromId(fieldId).getOptions(null);
330: return new DefaultComboBoxModel(options);
331: }
332:
333: public ReferenceNodeModelCache getReferenceCache(boolean task) {
334: // DocumentFrame df = ((MainFrame) owner).getCurrentFrame();
335: DocumentFrame df = GraphicManager.getInstance(this )
336: .getCurrentFrame();
337: return df.getReferenceCache(task);
338: }
339:
340: public NodeModelCache createCache(boolean task, String viewName) {
341: // DocumentFrame df = ((MainFrame) owner).getCurrentFrame();
342: DocumentFrame df = GraphicManager.getInstance(this )
343: .getCurrentFrame();
344: return df.createCache(task, viewName);
345: }
346:
347: protected boolean hasOkAndCancelButtons() {
348: return !hasCloseButton();
349: }
350:
351: protected boolean hasCloseButton() {
352: return false;
353: }
354:
355: public static JDialog containedInDialog(Object object) {
356: if (!(object instanceof Component))
357: return null;
358: Component c = (Component) object;
359: while (c != null) {
360: if (c instanceof JDialog)
361: return (JDialog) c;
362: c = c.getParent();
363: }
364: return null;
365: }
366:
367: public class DoubleClickRadio extends JRadioButton implements
368: MouseListener {
369: private static final long serialVersionUID = 1L;
370:
371: public DoubleClickRadio(String label, String tooltip) {
372: super (label);
373: this .setToolTipText(tooltip);
374: addMouseListener(this );
375: }
376:
377: public Point getToolTipLocation(MouseEvent event) { // the tip MUST be touching the button if html because you can click on links
378: return new Point(getWidth() - 2, -20);
379: }
380:
381: public JToolTip createToolTip() {
382: JToolTip tip = new HyperLinkToolTip();
383: tip.setComponent(this );
384: return tip;
385: }
386:
387: public void mouseClicked(MouseEvent e) {
388: if (e.getClickCount() == 2) {
389: bind(false);
390: ((JRadioButton) e.getSource()).setSelected(true);
391: AbstractDialog.this .onOk();
392: }
393: }
394:
395: public void mousePressed(MouseEvent e) {
396: }
397:
398: public void mouseReleased(MouseEvent e) {
399: }
400:
401: public void mouseEntered(MouseEvent e) {
402: }
403:
404: public void mouseExited(MouseEvent e) {
405: }
406: }
407:
408: public int getDialogResult() {
409: return dialogResult;
410: }
411:
412: public void setDialogResult(int dialogResult) {
413: this .dialogResult = dialogResult;
414: }
415:
416: public ButtonPanel getButtonPanel() {
417: return buttonPanel;
418: }
419:
420: public void setButtonPanel(ButtonPanel buttonPanel) {
421: this .buttonPanel = buttonPanel;
422: }
423:
424: public JComponent getContentPanel() {
425: return contentPanel;
426: }
427:
428: public void setContentPanel(JComponent contentPanel) {
429: this .contentPanel = contentPanel;
430: }
431:
432: public void addDocHelp(String helpAddress) {
433: HelpUtil.addDocHelp(this , helpAddress);
434: this .helpAddress = helpAddress;
435: }
436: // protected JComponent createButtonPanel() {
437: // JPanel panel = new JPanel(new BasicOptionPaneUI.ButtonAreaLayout(true, 6)) {
438: // public Dimension getMaximumSize() {
439: // return getPreferredSize();
440: // }
441: // };
442: //
443: // panel.setBorder(BorderFactory.createEmptyBorder(9, 0, 0, 0));
444: //
445: //
446: // Action findAction = getAction(EXECUTE_ACTION_COMMAND);
447: // Action closeAction = getAction(CLOSE_ACTION_COMMAND);
448: //
449: // JButton findButton = new JButton(findAction);
450: // panel.add(findButton);
451: // if (findAction != closeAction) {
452: // panel.add(new JButton(closeAction));
453: // }
454: //
455: //
456: // KeyStroke enterKey = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false);
457: // KeyStroke escapeKey = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
458: //
459: // InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
460: // inputMap.put(enterKey, EXECUTE_ACTION_COMMAND);
461: // inputMap.put(escapeKey, CLOSE_ACTION_COMMAND);
462: //
463: // getRootPane().setDefaultButton(findButton);
464: // return panel;
465: // }
466: }
|