001: /*
002: * EditWindow.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.components;
013:
014: import java.awt.BorderLayout;
015: import java.awt.Dialog;
016: import java.awt.Dimension;
017: import java.awt.FlowLayout;
018: import java.awt.Frame;
019: import java.awt.event.ActionEvent;
020: import java.awt.event.ActionListener;
021: import java.awt.event.WindowListener;
022: import javax.swing.JButton;
023: import javax.swing.JComponent;
024: import javax.swing.JDialog;
025: import javax.swing.JPanel;
026: import workbench.WbManager;
027: import workbench.gui.WbSwingUtilities;
028: import workbench.gui.actions.EscAction;
029: import workbench.gui.sql.EditorPanel;
030: import workbench.interfaces.Restoreable;
031: import workbench.interfaces.TextContainer;
032: import workbench.resource.ResourceMgr;
033: import workbench.resource.Settings;
034:
035: /**
036: *
037: * @author support@sql-workbench.net
038: */
039: public class EditWindow extends JDialog implements ActionListener,
040: WindowListener {
041: private TextContainer textContainer;
042: private JComponent editor;
043: private Restoreable componentSettings;
044: private JButton okButton = new WbButton(ResourceMgr
045: .getString("LblOK"));
046: private JButton cancelButton = new WbButton(ResourceMgr
047: .getString("LblCancel"));
048: private boolean isCancelled = true;
049: private String settingsId = null;
050:
051: public EditWindow(Frame owner, String title, String text) {
052: this (owner, title, text, "workbench.data.edit.window");
053: }
054:
055: public EditWindow(Frame owner, String title, String text,
056: boolean createSqlEditor, boolean showCloseButtonOnly) {
057: this (owner, title, text, "workbench.data.edit.window",
058: createSqlEditor, true, showCloseButtonOnly);
059: }
060:
061: public EditWindow(Frame owner, String title, String text,
062: String settingsId) {
063: this (owner, title, text, settingsId, false);
064: }
065:
066: public EditWindow(Frame owner, String title, String text,
067: String settingsId, boolean createSqlEditor) {
068: this (owner, title, text, settingsId, createSqlEditor, true,
069: false);
070: }
071:
072: public EditWindow(Frame owner, String title, String text,
073: String settingsId, boolean createSqlEditor, boolean modal) {
074: this (owner, title, text, settingsId, createSqlEditor, modal,
075: false);
076: }
077:
078: public EditWindow(final Frame owner, final String title,
079: final String text, final String settingsId,
080: final boolean createSqlEditor, final boolean modal,
081: final boolean showCloseButtonOnly) {
082: super (owner, title, modal);
083: init(text, settingsId, createSqlEditor, showCloseButtonOnly);
084: WbSwingUtilities.center(this , owner);
085: }
086:
087: public EditWindow(final Dialog owner, final String title,
088: final String text, final String settingsId,
089: final boolean createSqlEditor) {
090: super (owner, title, true);
091: init(text, settingsId, createSqlEditor, false);
092: WbSwingUtilities.center(this , WbManager.getInstance()
093: .getCurrentWindow());
094: }
095:
096: public EditWindow(final Dialog owner, final String title,
097: final String text, final boolean createSqlEditor,
098: final boolean showCloseButtonOnly) {
099: super (owner, title, true);
100: init(text, "workbench.data.edit.window", createSqlEditor,
101: showCloseButtonOnly);
102: WbSwingUtilities.center(this , WbManager.getInstance()
103: .getCurrentWindow());
104: }
105:
106: public void setReadOnly() {
107: this .textContainer.setEditable(false);
108: }
109:
110: private void init(String text, String id, boolean createSqlEditor,
111: boolean showCloseButtonOnly) {
112: this .settingsId = id;
113: setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
114: this .getContentPane().setLayout(new BorderLayout());
115: if (createSqlEditor) {
116: EditorPanel panel = EditorPanel.createSqlEditor();
117: panel.showFindOnPopupMenu();
118: panel.showFormatSql();
119: this .editor = panel;
120: this .textContainer = panel;
121: } else {
122: if (Settings.getInstance().getUsePlainEditorForData()) {
123: PlainEditor ed = new PlainEditor(this );
124: ed.restoreSettings();
125: this .componentSettings = ed;
126: this .textContainer = ed;
127: this .editor = ed;
128: } else {
129: EditorPanel panel = EditorPanel.createTextEditor();
130: panel.showFindOnPopupMenu();
131: this .editor = panel;
132: this .textContainer = panel;
133: }
134: }
135:
136: this .getContentPane().add(editor, BorderLayout.CENTER);
137: JPanel buttonPanel = new JPanel();
138: buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
139: if (!showCloseButtonOnly) {
140: buttonPanel.add(this .okButton);
141: } else {
142: this .cancelButton
143: .setText(ResourceMgr.getString("LblClose"));
144: }
145: buttonPanel.add(this .cancelButton);
146: this .getContentPane().add(buttonPanel, BorderLayout.SOUTH);
147:
148: this .textContainer.setText(text);
149: this .editor.setMinimumSize(new Dimension(100, 100));
150: this .editor.setPreferredSize(new Dimension(300, 200));
151: this .textContainer.setCaretPosition(0);
152:
153: this .okButton.addActionListener(this );
154: this .cancelButton.addActionListener(this );
155:
156: WbTraversalPolicy pol = new WbTraversalPolicy();
157: pol.setDefaultComponent(editor);
158: pol.addComponent(editor);
159: pol.addComponent(this .okButton);
160: pol.addComponent(this .cancelButton);
161: this .setFocusTraversalPolicy(pol);
162: this .setFocusCycleRoot(false);
163:
164: EscAction escAction = new EscAction(this );
165: escAction.addToInputMap(getRootPane().getInputMap(
166: JComponent.WHEN_IN_FOCUSED_WINDOW), getRootPane()
167: .getActionMap());
168:
169: if (!Settings.getInstance().restoreWindowSize(this , settingsId)) {
170: this .setSize(500, 400);
171: }
172:
173: this .addWindowListener(this );
174: }
175:
176: public void setInfoText(String text) {
177: if (this .editor instanceof PlainEditor) {
178: ((PlainEditor) editor).setInfoText(text);
179: }
180: }
181:
182: public void hideCancelButton() {
183: this .cancelButton.removeActionListener(this );
184: this .cancelButton.setVisible(false);
185: }
186:
187: public void actionPerformed(ActionEvent e) {
188: if (e.getSource() == this .okButton) {
189: this .isCancelled = false;
190: } else if (e.getSource() == this .cancelButton) {
191: this .isCancelled = true;
192: }
193: closeWindow();
194: }
195:
196: private void closeWindow() {
197: setVisible(false);
198: dispose();
199: }
200:
201: public boolean isCancelled() {
202: return this .isCancelled;
203: }
204:
205: public String getText() {
206: return this .textContainer.getText();
207: }
208:
209: public void windowActivated(java.awt.event.WindowEvent e) {
210: }
211:
212: public void windowClosed(java.awt.event.WindowEvent e) {
213: Settings.getInstance().storeWindowSize(this , this .settingsId);
214: if (componentSettings != null)
215: componentSettings.saveSettings();
216: }
217:
218: public void windowClosing(java.awt.event.WindowEvent e) {
219: }
220:
221: public void windowDeactivated(java.awt.event.WindowEvent e) {
222: }
223:
224: public void windowDeiconified(java.awt.event.WindowEvent e) {
225: }
226:
227: public void windowIconified(java.awt.event.WindowEvent e) {
228: }
229:
230: public void windowOpened(java.awt.event.WindowEvent e) {
231: editor.requestFocusInWindow();
232: WbSwingUtilities.repaintLater(editor);
233: }
234:
235: }
|