001: /*
002: * ObjectDropperUI.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.dbobjects;
013:
014: import java.awt.EventQueue;
015: import java.awt.Frame;
016: import java.util.List;
017:
018: import javax.swing.JDialog;
019: import javax.swing.WindowConstants;
020:
021: import workbench.db.DbObject;
022: import workbench.gui.WbSwingUtilities;
023: import workbench.gui.components.NoSelectionModel;
024: import workbench.gui.components.WbButton;
025: import workbench.interfaces.ObjectDropper;
026: import workbench.log.LogMgr;
027: import workbench.resource.ResourceMgr;
028: import workbench.util.WbThread;
029:
030: /**
031: *
032: * @author support@sql-workbench.net
033: */
034: public class ObjectDropperUI extends javax.swing.JPanel {
035: protected JDialog dialog;
036: protected boolean cancelled;
037: protected boolean running;
038: protected ObjectDropper dropper;
039: private Thread dropThread;
040:
041: public ObjectDropperUI(ObjectDropper drop) {
042: dropper = drop;
043: initComponents();
044: }
045:
046: protected void doDrop() {
047: if (this .running)
048: return;
049: try {
050: this .running = true;
051: this .cancelled = false;
052: this .dropper.dropObjects();
053: } catch (Throwable ex) {
054: String msg = ex.getMessage();
055: WbSwingUtilities.showErrorMessage(this .dialog, msg);
056: } finally {
057: this .running = false;
058: }
059:
060: EventQueue.invokeLater(new Runnable() {
061: public void run() {
062: if (cancelled) {
063: dropButton.setEnabled(true);
064: } else {
065: dialog.setVisible(false);
066: dialog.dispose();
067: dialog = null;
068: }
069: }
070: });
071:
072: }
073:
074: public boolean dialogWasCancelled() {
075: return this .cancelled;
076: }
077:
078: protected void initDisplay() {
079: List<? extends DbObject> objects = this .dropper.getObjects();
080: int numNames = objects.size();
081:
082: String[] display = new String[numNames];
083: for (int i = 0; i < numNames; i++) {
084: display[i] = objects.get(i).getObjectType() + " "
085: + objects.get(i).getObjectName();
086: }
087: this .objectList.setListData(display);
088:
089: if (!dropper.supportsCascade()) {
090: this .mainPanel.remove(this .optionPanel);
091: this .checkBoxCascadeConstraints.setSelected(false);
092: }
093: }
094:
095: public void showDialog(Frame aParent) {
096: initDisplay();
097:
098: this .dialog = new JDialog(aParent, ResourceMgr
099: .getString("TxtDropObjectsTitle"), true);
100: try {
101: this .dialog
102: .setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
103: this .dialog.getContentPane().add(this );
104: this .dialog.pack();
105: if (this .dialog.getWidth() < 200) {
106: this .dialog.setSize(200, this .dialog.getHeight());
107: }
108: WbSwingUtilities.center(this .dialog, aParent);
109: this .cancelled = true;
110: this .dialog.setVisible(true);
111: } finally {
112: if (this .dialog != null) {
113: this .dialog.dispose();
114: this .dialog = null;
115: }
116: }
117: }
118:
119: /** This method is called from within the constructor to
120: * initialize the form.
121: * WARNING: Do NOT modify this code. The content of this method is
122: * always regenerated by the Form Editor.
123: */
124: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
125: private void initComponents() {
126: buttonPanel = new javax.swing.JPanel();
127: dropButton = new WbButton();
128: cancelButton = new WbButton();
129: mainPanel = new javax.swing.JPanel();
130: jScrollPane1 = new javax.swing.JScrollPane();
131: objectList = new javax.swing.JList();
132: optionPanel = new javax.swing.JPanel();
133: checkBoxCascadeConstraints = new javax.swing.JCheckBox();
134:
135: setLayout(new java.awt.BorderLayout());
136:
137: buttonPanel.setLayout(new java.awt.FlowLayout(
138: java.awt.FlowLayout.RIGHT));
139:
140: dropButton.setText(ResourceMgr.getString("LblDrop"));
141: dropButton
142: .addActionListener(new java.awt.event.ActionListener() {
143: public void actionPerformed(
144: java.awt.event.ActionEvent evt) {
145: dropButtonActionPerformed(evt);
146: }
147: });
148:
149: buttonPanel.add(dropButton);
150:
151: cancelButton.setText(ResourceMgr.getString("LblCancel"));
152: cancelButton
153: .addActionListener(new java.awt.event.ActionListener() {
154: public void actionPerformed(
155: java.awt.event.ActionEvent evt) {
156: cancelButtonActionPerformed(evt);
157: }
158: });
159:
160: buttonPanel.add(cancelButton);
161:
162: add(buttonPanel, java.awt.BorderLayout.SOUTH);
163:
164: mainPanel.setLayout(new java.awt.BorderLayout());
165:
166: objectList
167: .setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
168: objectList.setSelectionModel(new NoSelectionModel());
169: jScrollPane1.setViewportView(objectList);
170:
171: mainPanel.add(jScrollPane1, java.awt.BorderLayout.CENTER);
172:
173: optionPanel.setLayout(new java.awt.FlowLayout(
174: java.awt.FlowLayout.LEFT));
175:
176: checkBoxCascadeConstraints.setText(ResourceMgr
177: .getString("LblCascadeConstraints"));
178: checkBoxCascadeConstraints.setToolTipText(ResourceMgr
179: .getDescription("LblCascadeConstraints"));
180: optionPanel.add(checkBoxCascadeConstraints);
181:
182: mainPanel.add(optionPanel, java.awt.BorderLayout.SOUTH);
183:
184: add(mainPanel, java.awt.BorderLayout.CENTER);
185:
186: }// </editor-fold>//GEN-END:initComponents
187:
188: private void cancelButtonActionPerformed(
189: java.awt.event.ActionEvent evt)//GEN-FIRST:event_cancelButtonActionPerformed
190: {//GEN-HEADEREND:event_cancelButtonActionPerformed
191: this .cancelled = true;
192: if (this .running) {
193: try {
194: dropper.cancel();
195: } catch (Exception e) {
196: LogMgr.logError("ObjectDropperUI",
197: "Error when cancelling drop", e);
198: }
199: dropButton.setEnabled(true);
200: } else {
201: this .dialog.setVisible(false);
202: this .dialog.dispose();
203: this .dialog = null;
204: }
205: }//GEN-LAST:event_cancelButtonActionPerformed
206:
207: private void dropButtonActionPerformed(
208: java.awt.event.ActionEvent evt)//GEN-FIRST:event_dropButtonActionPerformed
209: {//GEN-HEADEREND:event_dropButtonActionPerformed
210: if (this .running)
211: return;
212:
213: this .dropButton.setEnabled(false);
214: this .dropper
215: .setCascade(checkBoxCascadeConstraints.isSelected());
216:
217: dropThread = new WbThread("DropThread") {
218: public void run() {
219: doDrop();
220: }
221: };
222: dropThread.start();
223: }//GEN-LAST:event_dropButtonActionPerformed
224:
225: // Variables declaration - do not modify//GEN-BEGIN:variables
226: protected javax.swing.JPanel buttonPanel;
227: protected javax.swing.JButton cancelButton;
228: protected javax.swing.JCheckBox checkBoxCascadeConstraints;
229: protected javax.swing.JButton dropButton;
230: protected javax.swing.JScrollPane jScrollPane1;
231: protected javax.swing.JPanel mainPanel;
232: protected javax.swing.JList objectList;
233: protected javax.swing.JPanel optionPanel;
234: // End of variables declaration//GEN-END:variables
235:
236: }
|