1 package com.opensymphony.workflow.designer.dialogs; 2 3 import java.awt.event.*; 4 import java.awt.*; 5 import javax.swing.*; 6 import javax.swing.border.Border ; 7 8 import com.jgoodies.forms.factories.ButtonBarFactory; 9 import com.opensymphony.workflow.designer.swing.BannerPanel; 10 import com.opensymphony.workflow.designer.Utils; 11 import com.opensymphony.workflow.designer.ResourceManager; 12 13 18 public class BaseDialog extends JDialog 19 { 20 private BannerPanel banner; 21 private JPanel contentPane; 22 private boolean cancelClicked; 23 public final static Border WINDOW_BORDER = BorderFactory.createEmptyBorder(4, 10, 10, 10); 24 25 private Action okAction = new AbstractAction() 26 { 27 public void actionPerformed(ActionEvent e) 28 { 29 ok(); 30 } 31 }; 32 33 private Action cancelOrCloseAction = new AbstractAction() 34 { 35 public void actionPerformed(ActionEvent e) 36 { 37 cancel(); 38 } 39 }; 40 private static final Border CONTENT_BORDER = BorderFactory.createEmptyBorder(3, 3, 3, 3); 41 42 public BaseDialog() throws HeadlessException 43 { 44 super(); 45 buildUI(); 46 } 47 48 public BaseDialog(Frame owner) throws HeadlessException 49 { 50 super(owner); 51 buildUI(); 52 } 53 54 public BaseDialog(Frame owner, boolean modal) throws HeadlessException 55 { 56 super(owner, modal); 57 buildUI(); 58 } 59 60 public BaseDialog(Frame owner, String title) throws HeadlessException 61 { 62 super(owner, title); 63 buildUI(); 64 } 65 66 public BaseDialog(Frame owner, String title, boolean modal) throws HeadlessException 67 { 68 super(owner, title, modal); 69 buildUI(); 70 } 71 72 public final BannerPanel getBanner() 73 { 74 return banner; 75 } 76 77 public final Container getContentPane() 78 { 79 return contentPane; 80 } 81 82 85 public boolean ask() 86 { 87 show(); 88 return !cancelClicked; 89 } 90 91 94 public boolean ask(Component parent) 95 { 96 show(parent); 97 return !cancelClicked; 98 } 99 100 protected void ok() 101 { 102 cancelClicked = false; 103 setVisible(false); 104 } 105 106 protected void cancel() 107 { 108 cancelClicked = true; 109 setVisible(false); 110 } 111 112 private void buildUI() 113 { 114 JPanel container = (JPanel)super.getContentPane(); 115 container.setLayout(new BorderLayout(0, 0)); 116 banner = new BannerPanel(); 117 container.add(banner, BorderLayout.NORTH); 118 119 JPanel mainPanel = new JPanel(); 120 mainPanel.setBorder(WINDOW_BORDER); 121 mainPanel.setLayout(new BorderLayout()); 122 container.add(mainPanel, BorderLayout.CENTER); 123 124 contentPane = new JPanel(); 125 contentPane.setLayout(new BorderLayout(3, 3)); 126 contentPane.setBorder(CONTENT_BORDER); 127 mainPanel.add(contentPane, BorderLayout.CENTER); 128 129 JButton ok = new JButton(ResourceManager.getString("ok")); 130 ok.setDefaultCapable(true); 131 getRootPane().setDefaultButton(ok); 132 ok.addActionListener(okAction); 133 JButton cancel = new JButton(ResourceManager.getString("cancel")); 134 cancel.addActionListener(cancelOrCloseAction); 135 mainPanel.add(ButtonBarFactory.buildOKCancelBar(ok, cancel), BorderLayout.SOUTH); 136 137 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); 138 139 container.getActionMap().put("cancel", cancelOrCloseAction); 140 container.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "cancel"); 141 addWindowListener(new WindowAdapter() 142 { 143 public void windowClosing(WindowEvent e) 144 { 145 cancel(); 146 } 147 }); 148 } 149 150 156 public void show(Component parent) 157 { 158 pack(); 159 if(parent == null) 160 { 161 Utils.centerComponent(this); 162 } 163 else 164 { 165 Utils.centerComponent(parent, this); 166 } 167 super.show(); 168 } 169 170 } 171 | Popular Tags |