001: /*
002: * SQLeonardo :: java database frontend
003: * Copyright (C) 2004 nickyb@users.sourceforge.net
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: *
019: */
020:
021: package nickyb.sqleonardo.environment.ctrl.content;
022:
023: import javax.swing.JCheckBox;
024:
025: import nickyb.sqleonardo.common.gui.AbstractDialogWizard;
026: import nickyb.sqleonardo.common.gui.AbstractMaskChooser;
027: import nickyb.sqleonardo.environment.Application;
028: import nickyb.sqleonardo.environment.Preferences;
029: import nickyb.sqleonardo.environment.ctrl.ContentPane;
030: import nickyb.sqleonardo.environment.mdi.DefaultMaskChooser;
031:
032: public class DialogStream extends AbstractDialogWizard {
033: private String tname = null;
034: private ContentView view;
035:
036: private JCheckBox cbxClose;
037:
038: private AbstractMaskPerform mkp;
039: private AbstractMaskChooser mkc;
040:
041: private TaskOwner task;
042:
043: private DialogStream(String title, ContentView view, String tname) {
044: super (Application.window, title);
045:
046: this .tname = tname;
047: this .view = view;
048:
049: cbxClose = new JCheckBox("close dialog when finished",
050: Preferences.getBoolean("content.close-dialog", false));
051: cbxClose.setVisible(false);
052: bar.add(cbxClose, 0);
053: }
054:
055: public static void showExport(ContentPane content) {
056: String tname = null;
057: new DialogStream("export", content.getView(), tname).show();
058: }
059:
060: public static void showImport(ContentPane content) {
061: String tname = null;
062: new DialogStream("import", content.getView(), tname).show();
063: }
064:
065: public void dispose() {
066: Preferences.set("content.close-dialog", new Boolean(cbxClose
067: .isSelected()));
068: super .dispose();
069: }
070:
071: protected boolean onBack() {
072: cbxClose.setVisible(false);
073: return super .onBack();
074: }
075:
076: protected boolean onNext() {
077: cbxClose.setVisible(true);
078:
079: if (getStep() == 0) {
080: if (mkc.getSelectedFile() != null) {
081: mkp.setType(mkc.getPerformType(), tname, mkc
082: .getSelectedFile().toString());
083: mkp.setContent(view);
084:
085: return true;
086: }
087: } else {
088: new Thread(task = new TaskOwner()).start();
089: }
090:
091: return false;
092: }
093:
094: protected void onOpen() {
095: if (this .getTitle().equals("export")) {
096: mkp = new MaskExport();
097: mkc = new DefaultMaskChooser(
098: AbstractMaskChooser.SAVE_DIALOG,
099: AbstractMaskChooser.FILES_ONLY, false);
100:
101: mkc.addChoosableFileFilter(new SQLFilter());
102: mkc.addChoosableFileFilter(new WebFilter());
103:
104: } else if (this .getTitle().equals("import")) {
105: mkp = new MaskImport();
106: mkc = new DefaultMaskChooser(
107: AbstractMaskChooser.OPEN_DIALOG,
108: AbstractMaskChooser.FILES_ONLY, false);
109: }
110:
111: mkc.addChoosableFileFilter(new TXTFilter());
112:
113: addStep(mkc);
114: addStep(mkp);
115:
116: getContentPane().validate();
117: }
118:
119: private class TaskOwner implements Runnable {
120: public void run() {
121: DialogStream.this .setBarEnabled(false);
122: DialogStream.this .mkp.setEnabled(false);
123:
124: DialogStream.this .mkp.init();
125: while (DialogStream.this .task != null
126: && !DialogStream.this .mkp.aborted()
127: && !DialogStream.this .mkp.finished()) {
128: DialogStream.this .mkp.next();
129: }
130:
131: DialogStream.this .mkp.setEnabled(true);
132: DialogStream.this .setBarEnabled(true);
133:
134: if (DialogStream.this .mkp.finished()
135: && cbxClose.isSelected())
136: DialogStream.this .dispose();
137: }
138: }
139:
140: private static class SQLFilter extends
141: AbstractMaskChooser.AbstractFileFilter {
142: SQLFilter() {
143: super ("insert statements", new String[] { ".sql" });
144: }
145:
146: public short getPerformType() {
147: return MaskExport.SQL;
148: }
149: }
150:
151: private static class TXTFilter extends
152: AbstractMaskChooser.AbstractFileFilter {
153: TXTFilter() {
154: super ("text files", new String[] { ".txt" });
155: }
156:
157: public short getPerformType() {
158: return MaskExport.TXT;
159: }
160: }
161:
162: private static class WebFilter extends
163: AbstractMaskChooser.AbstractFileFilter {
164: WebFilter() {
165: super ("web pages", new String[] { ".htm", ".html" });
166: }
167:
168: public short getPerformType() {
169: return MaskExport.WEB;
170: }
171: }
172: }
|