| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hivegui.dialog; |
| 16 | |
| 17 | import java.awt.event.WindowAdapter; |
| 18 | import java.awt.event.WindowEvent; |
| 19 | |
| 20 | import javax.swing.JDialog; |
| 21 | import javax.swing.JFrame; |
| 22 | |
| 23 | import org.jdesktop.application.ApplicationContext; |
| 24 | |
| 25 | /** |
| 26 | * @author Jean-Francois Poilpret |
| 27 | */ |
| 28 | public class GenericDialog extends JDialog |
| 29 | { |
| 30 | public GenericDialog(JDialog parent, DialogPanel panel) |
| 31 | { |
| 32 | super(parent, true); |
| 33 | _panel = panel; |
| 34 | } |
| 35 | |
| 36 | public GenericDialog(JFrame parent, DialogPanel panel) |
| 37 | { |
| 38 | super(parent, true); |
| 39 | _panel = panel; |
| 40 | } |
| 41 | |
| 42 | public void setContext(ApplicationContext context) |
| 43 | { |
| 44 | _context = context; |
| 45 | String name = _panel.getPanel().getName(); |
| 46 | setName(name + "-dialog"); |
| 47 | setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); |
| 48 | addWindowListener(new CloseListener()); |
| 49 | init(name); |
| 50 | // pack(); |
| 51 | } |
| 52 | |
| 53 | protected void init(String name) |
| 54 | { |
| 55 | if (_panel.getPanel().getClientProperty(DIALOGPANEL_INITED) == null) |
| 56 | { |
| 57 | _panel.setContext(_context); |
| 58 | _panel.getPanel().putClientProperty(DIALOGPANEL_INITED, "true"); |
| 59 | } |
| 60 | _panel.setParent(this); |
| 61 | _panel.reset(); |
| 62 | setTitle(_panel.getDialogTitle()); |
| 63 | // Workaround a bug with JFileDialog and substance: |
| 64 | // use add() instead of setCOntentPane() |
| 65 | // setContentPane(_panel.getPanel()); |
| 66 | add(_panel.getPanel()); |
| 67 | if (_panel.getDefaultButton() != null) |
| 68 | { |
| 69 | getRootPane().setDefaultButton(_panel.getDefaultButton()); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // Should be called back by DialogPanel to close the dialog |
| 74 | public void close(boolean cancel) |
| 75 | { |
| 76 | _cancelled = cancel; |
| 77 | setVisible(false); |
| 78 | dispose(); |
| 79 | } |
| 80 | |
| 81 | public boolean wasCancelled() |
| 82 | { |
| 83 | return _cancelled; |
| 84 | } |
| 85 | |
| 86 | private class CloseListener extends WindowAdapter |
| 87 | { |
| 88 | @Override public void windowClosing(WindowEvent event) |
| 89 | { |
| 90 | if (_panel.canClose()) |
| 91 | { |
| 92 | setVisible(false); |
| 93 | dispose(); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | final protected DialogPanel _panel; |
| 99 | protected ApplicationContext _context = null; |
| 100 | protected boolean _cancelled = true; |
| 101 | |
| 102 | static final private String DIALOGPANEL_INITED = "GenericDialog.inited"; |
| 103 | } |