Browse Source

Initial work on componentising DCC plugin (and all windows)

Change-Id: I2d54aeab876cb60d63d8fd5445d5344aee914054
Reviewed-on: http://gerrit.dmdirc.com/2042
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
tags/0.7rc1
Greg Holmes 13 years ago
parent
commit
c55ab4f18d

+ 15
- 1
src/com/dmdirc/addons/dcc/DCCPlugin.java View File

@@ -32,7 +32,10 @@ import com.dmdirc.addons.dcc.io.DCC;
32 32
 import com.dmdirc.addons.dcc.io.DCCChat;
33 33
 import com.dmdirc.addons.dcc.io.DCCTransfer;
34 34
 import com.dmdirc.addons.dcc.kde.KFileChooser;
35
+import com.dmdirc.addons.dcc.ui.PlaceholderWindow;
36
+import com.dmdirc.addons.dcc.ui.TransferWindow;
35 37
 import com.dmdirc.addons.ui_swing.SwingController;
38
+import com.dmdirc.addons.ui_swing.SwingWindowFactory;
36 39
 import com.dmdirc.config.Identity;
37 40
 import com.dmdirc.config.IdentityManager;
38 41
 import com.dmdirc.config.prefs.PluginPreferencesCategory;
@@ -53,12 +56,14 @@ import java.io.File;
53 56
 import java.io.IOException;
54 57
 import java.net.InetAddress;
55 58
 import java.net.UnknownHostException;
59
+import java.util.Arrays;
60
+import java.util.HashSet;
56 61
 
57 62
 import javax.swing.JFileChooser;
58 63
 import javax.swing.JOptionPane;
59 64
 
60 65
 /**
61
- * This plugin adds DCC to dmdirc.
66
+ * This plugin adds DCC to DMDirc.
62 67
  */
63 68
 public final class DCCPlugin extends BasePlugin implements ActionListener {
64 69
 
@@ -69,6 +74,15 @@ public final class DCCPlugin extends BasePlugin implements ActionListener {
69 74
     public DCCPlugin() {
70 75
         super();
71 76
         registerCommand(new DCCCommand(this), DCCCommand.INFO);
77
+        final SwingWindowFactory factory = ((SwingController) PluginManager
78
+                .getPluginManager().getPluginInfoByName("ui_swing").getPlugin())
79
+                .getWindowFactory();
80
+        factory.registerImplementation(new HashSet<String>(Arrays.asList(
81
+                "com.dmdirc.addons.dcc.ui.PlaceholderPanel")),
82
+                PlaceholderWindow.class);
83
+        factory.registerImplementation(new HashSet<String>(Arrays.asList(
84
+                "com.dmdirc.addons.dcc.ui.TransferPanel")),
85
+                TransferWindow.class);
72 86
     }
73 87
 
74 88
     /**

+ 4
- 3
src/com/dmdirc/addons/dcc/PlaceholderContainer.java View File

@@ -31,7 +31,7 @@ import com.dmdirc.config.IdentityManager;
31 31
 import com.dmdirc.plugins.PluginManager;
32 32
 
33 33
 import java.awt.Dialog.ModalityType;
34
-import java.util.Collections;
34
+import java.util.Arrays;
35 35
 
36 36
 /**
37 37
  * Creates a placeholder DCC Frame.
@@ -42,13 +42,14 @@ public class PlaceholderContainer extends FrameContainer {
42 42
     private final DCCPlugin plugin;
43 43
 
44 44
     /**
45
-     * Creates a placeholder dcc frame.
45
+     * Creates a placeholder DCC frame.
46 46
      *
47 47
      * @param plugin The plugin which owns this placeholder
48 48
      */
49 49
     public PlaceholderContainer(final DCCPlugin plugin) {
50 50
         super("dcc", "DCCs", "DCCs", PlaceholderWindow.class,
51
-                IdentityManager.getGlobalConfig(), Collections.<String>emptyList());
51
+                IdentityManager.getGlobalConfig(),
52
+                Arrays.asList("com.dmdirc.addons.dcc.ui.PlaceholderPanel"));
52 53
 
53 54
         this.plugin = plugin;
54 55
     }

+ 2
- 2
src/com/dmdirc/addons/dcc/TransferContainer.java View File

@@ -37,7 +37,7 @@ import com.dmdirc.ui.WindowManager;
37 37
 
38 38
 import java.awt.Desktop;
39 39
 import java.io.File;
40
-import java.util.Collections;
40
+import java.util.Arrays;
41 41
 import java.util.Date;
42 42
 
43 43
 import javax.swing.JOptionPane;
@@ -95,7 +95,7 @@ public class TransferContainer extends FrameContainer implements
95 95
                 ? "dcc-send-inactive" : "dcc-receive-inactive",
96 96
                 title, title, TransferWindow.class,
97 97
                 IdentityManager.getGlobalConfig(),
98
-                Collections.<String>emptyList());
98
+                Arrays.asList("com.dmdirc.addons.dcc.ui.TransferPanel"));
99 99
         this.plugin = plugin;
100 100
         this.dcc = dcc;
101 101
         this.server = server;

+ 51
- 0
src/com/dmdirc/addons/dcc/ui/PlaceholderPanel.java View File

@@ -0,0 +1,51 @@
1
+/*
2
+ * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.addons.dcc.ui;
24
+
25
+import com.dmdirc.addons.ui_swing.components.text.TextLabel;
26
+
27
+import javax.swing.JPanel;
28
+
29
+import net.miginfocom.swing.MigLayout;
30
+
31
+/**
32
+ * A panel which serves as a top-level placeholder for other DCC windows.
33
+ *
34
+ * @since 0.6.6
35
+ */
36
+public class PlaceholderPanel extends JPanel {
37
+
38
+    /** A version number for this class. */
39
+    private static final long serialVersionUID = 1L;
40
+
41
+    /**
42
+     * Creates a new placeholder window for the specified UI controller and
43
+     * owner.
44
+     */
45
+    public PlaceholderPanel() {
46
+        super(new MigLayout("fill, alignx center, aligny center"));
47
+        add(new TextLabel(
48
+                "This is a placeholder window to group DCCs together."
49
+                + "\n\nClosing this window will close all the active DCCs"));
50
+    }
51
+}

+ 1
- 6
src/com/dmdirc/addons/dcc/ui/PlaceholderWindow.java View File

@@ -24,13 +24,11 @@ package com.dmdirc.addons.dcc.ui;
24 24
 
25 25
 import com.dmdirc.FrameContainer;
26 26
 import com.dmdirc.addons.ui_swing.SwingController;
27
-import com.dmdirc.addons.ui_swing.components.text.TextLabel;
28 27
 
29 28
 /**
30 29
  * A virtually empty window which serves as a top-level placeholder for other
31 30
  * DCC windows.
32 31
  *
33
- * @author chris
34 32
  * @since 0.6.4
35 33
  */
36 34
 public class PlaceholderWindow extends EmptyWindow {
@@ -47,10 +45,7 @@ public class PlaceholderWindow extends EmptyWindow {
47 45
     public PlaceholderWindow(final SwingController controller,
48 46
             final FrameContainer owner) {
49 47
         super(controller, owner);
50
-
51
-        add(new TextLabel(
52
-                "This is a placeholder window to group DCCs together."
53
-                + "\n\nClosing this window will close all the active DCCs"));
48
+        add(new PlaceholderPanel());
54 49
     }
55 50
 
56 51
 }

+ 277
- 0
src/com/dmdirc/addons/dcc/ui/TransferPanel.java View File

@@ -0,0 +1,277 @@
1
+/*
2
+ * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.addons.dcc.ui;
24
+
25
+import com.dmdirc.FrameContainer;
26
+import com.dmdirc.addons.dcc.DCCTransferHandler;
27
+import com.dmdirc.addons.dcc.TransferContainer;
28
+import com.dmdirc.addons.dcc.io.DCCTransfer;
29
+import com.dmdirc.addons.ui_swing.UIUtilities;
30
+import com.dmdirc.logger.ErrorLevel;
31
+import com.dmdirc.logger.Logger;
32
+import com.dmdirc.parser.interfaces.Parser;
33
+import com.dmdirc.parser.interfaces.callbacks.SocketCloseListener;
34
+
35
+import java.awt.Desktop;
36
+import java.awt.event.ActionEvent;
37
+import java.awt.event.ActionListener;
38
+import java.io.File;
39
+import java.io.IOException;
40
+import java.util.Date;
41
+
42
+import javax.swing.JButton;
43
+import javax.swing.JLabel;
44
+import javax.swing.JPanel;
45
+import javax.swing.JProgressBar;
46
+
47
+import net.miginfocom.swing.MigLayout;
48
+
49
+/**
50
+ * A panel for displaying the progress of DCC transfers.
51
+ *
52
+ * @since 0.6.6
53
+ */
54
+public class TransferPanel extends JPanel implements ActionListener,
55
+        SocketCloseListener, DCCTransferHandler {
56
+
57
+    /** A version number for this class. */
58
+    private static final long serialVersionUID = 1L;
59
+    /** Parent container. */
60
+    private final TransferContainer transferContainer;
61
+    /** Progress Bar */
62
+    private final JProgressBar progress = new JProgressBar();
63
+    /** Status Label */
64
+    private final JLabel status = new JLabel("Status: Waiting");
65
+    /** Speed Label */
66
+    private final JLabel speed = new JLabel("Speed: Unknown");
67
+    /** Time Label */
68
+    private final JLabel remaining = new JLabel("Time Remaining: Unknown");
69
+    /** Time Taken */
70
+    private final JLabel taken = new JLabel("Time Taken: 00:00");
71
+    /** Button */
72
+    private final JButton button = new JButton("Cancel");
73
+    /** Open Button */
74
+    private final JButton openButton = new JButton("Open");
75
+    /** The transfer that this window is showing. */
76
+    private final DCCTransfer dcc;
77
+
78
+    /**
79
+     * Creates a new transfer window for the specified UI controller and owner.
80
+     *
81
+     * @param owner The frame container that owns this window
82
+     */
83
+    public TransferPanel(final FrameContainer owner) {
84
+        super();
85
+
86
+        this.transferContainer = (TransferContainer) owner;
87
+        dcc = transferContainer.getDCC();
88
+
89
+        dcc.addHandler(this);
90
+        transferContainer.addSocketCloseCallback(this);
91
+
92
+        setLayout(new MigLayout("hidemode 0"));
93
+
94
+        if (dcc.getType() == DCCTransfer.TransferType.SEND) {
95
+            add(new JLabel("Sending: " + dcc.getShortFileName()), "wrap");
96
+            add(new JLabel("To: " + transferContainer
97
+                    .getOtherNickname()), "wrap");
98
+        } else {
99
+            add(new JLabel("Recieving: " + dcc.getShortFileName()), "wrap");
100
+            add(new JLabel("From: " + transferContainer
101
+                    .getOtherNickname()), "wrap");
102
+        }
103
+
104
+        add(status, "wrap");
105
+        add(speed, "wrap");
106
+        add(remaining, "wrap");
107
+        add(taken, "wrap");
108
+        add(progress, "growx, wrap");
109
+
110
+        button.addActionListener(this);
111
+        openButton.addActionListener(this);
112
+        openButton.setVisible(false);
113
+
114
+        add(openButton, "split 2, align right");
115
+        add(button, "align right");
116
+    }
117
+
118
+    /**
119
+     * {@inheritDoc}
120
+     *
121
+     * @param e Action event
122
+     */
123
+    @Override
124
+    public void actionPerformed(final ActionEvent e) {
125
+        if (e.getActionCommand().equals("Cancel")) {
126
+            if (dcc.getType() == DCCTransfer.TransferType.SEND) {
127
+                button.setText("Resend");
128
+            } else {
129
+                button.setText("Close Window");
130
+            }
131
+            status.setText("Status: Cancelled");
132
+            dcc.close();
133
+        } else if (e.getActionCommand().equals("Resend")) {
134
+            button.setText("Cancel");
135
+            status.setText("Status: Resending...");
136
+
137
+            if (!(transferContainer.resend())) {
138
+                status.setText("Status: Resend failed.");
139
+                button.setText("Close Window");
140
+            }
141
+        } else if (e.getActionCommand().equals("Close Window")) {
142
+            transferContainer.close();
143
+        } else if (e.getSource() == openButton) {
144
+            final File file = new File(dcc.getFileName());
145
+            try {
146
+                Desktop.getDesktop().open(file);
147
+            } catch (IllegalArgumentException ex) {
148
+                Logger.userError(ErrorLevel.LOW, "Unable to open file: " + file, ex);
149
+                openButton.setEnabled(false);
150
+            } catch (IOException ex) {
151
+                try {
152
+                    Desktop.getDesktop().open(file.getParentFile());
153
+                } catch (IllegalArgumentException ex1) {
154
+                    Logger.userError(ErrorLevel.LOW, "Unable to open folder: " +
155
+                            file.getParentFile(), ex1);
156
+                    openButton.setEnabled(false);
157
+                } catch (IOException ex1) {
158
+                    Logger.userError(ErrorLevel.LOW, "No associated handler " +
159
+                            "to open file or directory.", ex1);
160
+                    openButton.setEnabled(false);
161
+                }
162
+            }
163
+        }
164
+    }
165
+
166
+    /** {@inheritDoc} */
167
+    @Override
168
+    public void onSocketClosed(final Parser parser, final Date date) {
169
+        if ("Resend".equals(button.getText())) {
170
+            button.setText("Close Window");
171
+        }
172
+    }
173
+
174
+    /** {@inheritDoc} */
175
+    @Override
176
+    public void socketClosed(final DCCTransfer dcc) {
177
+        UIUtilities.invokeLater(new Runnable() {
178
+
179
+            /** {@inheritDoc} */
180
+            @Override
181
+            public void run() {
182
+                if (transferContainer.isComplete()) {
183
+                    status.setText("Status: Transfer Compelete.");
184
+
185
+                    if (transferContainer.shouldShowOpenButton()) {
186
+                        openButton.setVisible(true);
187
+                    }
188
+
189
+                    progress.setValue(100);
190
+                    button.setText("Close Window");
191
+                } else {
192
+                    status.setText("Status: Transfer Failed.");
193
+                    if (dcc.getType() == DCCTransfer.TransferType.SEND) {
194
+                        button.setText("Resend");
195
+                    } else {
196
+                        button.setText("Close Window");
197
+                    }
198
+                }
199
+            }
200
+        });
201
+    }
202
+
203
+    /** {@inheritDoc} */
204
+    @Override
205
+    public void socketOpened(final DCCTransfer dcc) {
206
+        UIUtilities.invokeLater(new Runnable() {
207
+
208
+            /** {@inheritDoc} */
209
+            @Override
210
+            public void run() {
211
+                status.setText("Status: Socket Opened");
212
+            }
213
+
214
+        });
215
+    }
216
+
217
+    /** {@inheritDoc} */
218
+    @Override
219
+    public void dataTransfered(final DCCTransfer dcc, final int bytes) {
220
+        UIUtilities.invokeLater(new Runnable() {
221
+
222
+            /** {@inheritDoc} */
223
+            @Override
224
+            public void run() {
225
+                if (dcc.getType() == DCCTransfer.TransferType.SEND) {
226
+                    status.setText("Status: Sending");
227
+                } else {
228
+                    status.setText("Status: Recieving");
229
+                }
230
+
231
+                progress.setValue((int) transferContainer.getPercent());
232
+
233
+                final double bytesPerSecond = transferContainer
234
+                        .getBytesPerSecond();
235
+
236
+                if (bytesPerSecond > 1048576) {
237
+                    speed.setText(String.format("Speed: %.2f MiB/s",
238
+                            bytesPerSecond / 1048576));
239
+                } else if (bytesPerSecond > 1024) {
240
+                    speed.setText(String.format("Speed: %.2f KiB/s",
241
+                            bytesPerSecond / 1024));
242
+                } else {
243
+                    speed.setText(String.format("Speed: %.2f B/s",
244
+                            bytesPerSecond));
245
+                }
246
+
247
+                remaining.setText(String.format("Time Remaining: %s",
248
+                        duration((int) transferContainer.getRemainingTime())));
249
+                taken.setText(String.format("Time Taken: %s", transferContainer
250
+                        .getStartTime() == 0 ? "N/A" : duration(
251
+                        transferContainer.getElapsedTime())));
252
+            }
253
+        });
254
+    }
255
+
256
+    /**
257
+     * Get the duration in seconds as a string.
258
+     *
259
+     * @param secondsInput to get duration for
260
+     * @return Duration as a string
261
+     */
262
+    private String duration(final long secondsInput) {
263
+        final StringBuilder result = new StringBuilder();
264
+        final long hours = (secondsInput / 3600);
265
+        final long minutes = (secondsInput / 60 % 60);
266
+        final long seconds = (secondsInput % 60);
267
+
268
+        if (hours > 0) {
269
+            result.append(hours);
270
+            result.append(":");
271
+        }
272
+        result.append(String.format("%0,2d:%0,2d", minutes, seconds));
273
+
274
+        return result.toString();
275
+    }
276
+
277
+}

+ 4
- 233
src/com/dmdirc/addons/dcc/ui/TransferWindow.java View File

@@ -23,256 +23,27 @@
23 23
 package com.dmdirc.addons.dcc.ui;
24 24
 
25 25
 import com.dmdirc.FrameContainer;
26
-import com.dmdirc.addons.dcc.DCCTransferHandler;
27
-import com.dmdirc.addons.dcc.TransferContainer;
28
-import com.dmdirc.addons.dcc.io.DCCTransfer;
29 26
 import com.dmdirc.addons.ui_swing.SwingController;
30
-import com.dmdirc.addons.ui_swing.UIUtilities;
31
-import com.dmdirc.logger.ErrorLevel;
32
-import com.dmdirc.logger.Logger;
33
-import com.dmdirc.parser.interfaces.Parser;
34
-import com.dmdirc.parser.interfaces.callbacks.SocketCloseListener;
35
-
36
-import java.awt.Desktop;
37
-import java.awt.event.ActionEvent;
38
-import java.awt.event.ActionListener;
39
-import java.io.File;
40
-import java.io.IOException;
41
-import java.util.Date;
42
-
43
-import javax.swing.JButton;
44
-import javax.swing.JLabel;
45
-import javax.swing.JProgressBar;
46
-
47
-import net.miginfocom.swing.MigLayout;
48 27
 
49 28
 /**
50 29
  * A window for displaying the progress of DCC transfers.
51 30
  *
52
- * @author chris
53 31
  * @since 0.6.4
54 32
  */
55
-public class TransferWindow extends EmptyWindow implements ActionListener,
56
-        SocketCloseListener, DCCTransferHandler {
33
+public class TransferWindow extends EmptyWindow {
57 34
 
58 35
     /** A version number for this class. */
59 36
     private static final long serialVersionUID = 1L;
60 37
 
61
-    /** Progress Bar */
62
-    private final JProgressBar progress = new JProgressBar();
63
-
64
-    /** Status Label */
65
-    private final JLabel status = new JLabel("Status: Waiting");
66
-
67
-    /** Speed Label */
68
-    private final JLabel speed = new JLabel("Speed: Unknown");
69
-
70
-    /** Time Label */
71
-    private final JLabel remaining = new JLabel("Time Remaining: Unknown");
72
-
73
-    /** Time Taken */
74
-    private final JLabel taken = new JLabel("Time Taken: 00:00");
75
-
76
-    /** Button */
77
-    private final JButton button = new JButton("Cancel");
78
-
79
-    /** Open Button */
80
-    private final JButton openButton = new JButton("Open");
81
-
82
-    /** The transfer that this window is showing. */
83
-    private final DCCTransfer dcc;
84
-
85 38
     /**
86 39
      * Creates a new transfer window for the specified UI controller and owner.
87 40
      *
88 41
      * @param controller The UIController that owns this window
89 42
      * @param owner The frame container that owns this window
90 43
      */
91
-    public TransferWindow(final SwingController controller, final FrameContainer owner) {
44
+    public TransferWindow(final SwingController controller,
45
+            final FrameContainer owner) {
92 46
         super(controller, owner);
93
-
94
-        final TransferContainer container = (TransferContainer) owner;
95
-        dcc = container.getDCC();
96
-
97
-        dcc.addHandler(this);
98
-        container.addSocketCloseCallback(this);
99
-
100
-        setLayout(new MigLayout("hidemode 0"));
101
-
102
-        if (dcc.getType() == DCCTransfer.TransferType.SEND) {
103
-            add(new JLabel("Sending: " + dcc.getShortFileName()), "wrap");
104
-            add(new JLabel("To: " + container.getOtherNickname()), "wrap");
105
-        } else {
106
-            add(new JLabel("Recieving: " + dcc.getShortFileName()), "wrap");
107
-            add(new JLabel("From: " + container.getOtherNickname()), "wrap");
108
-        }
109
-
110
-        add(status, "wrap");
111
-        add(speed, "wrap");
112
-        add(remaining, "wrap");
113
-        add(taken, "wrap");
114
-        add(progress, "growx, wrap");
115
-
116
-        button.addActionListener(this);
117
-        openButton.addActionListener(this);
118
-        openButton.setVisible(false);
119
-
120
-        add(openButton, "split 2, align right");
121
-        add(button, "align right");
122
-    }
123
-
124
-    /** {@inheritDoc} */
125
-    @Override
126
-    public void actionPerformed(final ActionEvent e) {
127
-        if (e.getActionCommand().equals("Cancel")) {
128
-            if (dcc.getType() == DCCTransfer.TransferType.SEND) {
129
-                button.setText("Resend");
130
-            } else {
131
-                button.setText("Close Window");
132
-            }
133
-            status.setText("Status: Cancelled");
134
-            dcc.close();
135
-        } else if (e.getActionCommand().equals("Resend")) {
136
-            button.setText("Cancel");
137
-            status.setText("Status: Resending...");
138
-
139
-            if (!((TransferContainer) frameParent).resend()) {
140
-                status.setText("Status: Resend failed.");
141
-                button.setText("Close Window");
142
-            }
143
-        } else if (e.getActionCommand().equals("Close Window")) {
144
-            getContainer().close();
145
-        } else if (e.getSource() == openButton) {
146
-            final File file = new File(dcc.getFileName());
147
-            try {
148
-                Desktop.getDesktop().open(file);
149
-            } catch (IllegalArgumentException ex) {
150
-                Logger.userError(ErrorLevel.LOW, "Unable to open file: " + file, ex);
151
-                openButton.setEnabled(false);
152
-            } catch (IOException ex) {
153
-                try {
154
-                    Desktop.getDesktop().open(file.getParentFile());
155
-                } catch (IllegalArgumentException ex1) {
156
-                    Logger.userError(ErrorLevel.LOW, "Unable to open folder: " +
157
-                            file.getParentFile(), ex1);
158
-                    openButton.setEnabled(false);
159
-                } catch (IOException ex1) {
160
-                    Logger.userError(ErrorLevel.LOW, "No associated handler " +
161
-                            "to open file or directory.", ex1);
162
-                    openButton.setEnabled(false);
163
-                }
164
-            }
165
-        }
166
-    }
167
-
168
-    /** {@inheritDoc} */
169
-    @Override
170
-    public void onSocketClosed(final Parser parser, final Date date) {
171
-        // Can't resend without the parser.
172
-        if ("Resend".equals(button.getText())) {
173
-            button.setText("Close Window");
174
-        }
47
+        add(new TransferPanel(owner));
175 48
     }
176
-
177
-    /** {@inheritDoc} */
178
-    @Override
179
-    public void socketClosed(final DCCTransfer dcc) {
180
-        UIUtilities.invokeLater(new Runnable() {
181
-
182
-            private final TransferContainer container = (TransferContainer) frameParent;
183
-
184
-            /** {@inheritDoc} */
185
-            @Override
186
-            public void run() {
187
-                if (container.isComplete()) {
188
-                    status.setText("Status: Transfer Compelete.");
189
-
190
-                    if (container.shouldShowOpenButton()) {
191
-                        openButton.setVisible(true);
192
-                    }
193
-
194
-                    progress.setValue(100);
195
-                    button.setText("Close Window");
196
-                } else {
197
-                    status.setText("Status: Transfer Failed.");
198
-                    if (dcc.getType() == DCCTransfer.TransferType.SEND) {
199
-                        button.setText("Resend");
200
-                    } else {
201
-                        button.setText("Close Window");
202
-                    }
203
-                }
204
-            }
205
-        });
206
-    }
207
-
208
-    /** {@inheritDoc} */
209
-    @Override
210
-    public void socketOpened(final DCCTransfer dcc) {
211
-        UIUtilities.invokeLater(new Runnable() {
212
-
213
-            /** {@inheritDoc} */
214
-            @Override
215
-            public void run() {
216
-                status.setText("Status: Socket Opened");
217
-            }
218
-
219
-        });
220
-    }
221
-
222
-    /** {@inheritDoc} */
223
-    @Override
224
-    public void dataTransfered(final DCCTransfer dcc, final int bytes) {
225
-        UIUtilities.invokeLater(new Runnable() {
226
-
227
-            private final TransferContainer container = (TransferContainer) frameParent;
228
-
229
-            /** {@inheritDoc} */
230
-            @Override
231
-            public void run() {
232
-                if (dcc.getType() == DCCTransfer.TransferType.SEND) {
233
-                    status.setText("Status: Sending");
234
-                } else {
235
-                    status.setText("Status: Recieving");
236
-                }
237
-
238
-                progress.setValue((int) container.getPercent());
239
-
240
-                final double bytesPerSecond = container.getBytesPerSecond();
241
-
242
-                if (bytesPerSecond > 1048576) {
243
-                    speed.setText(String.format("Speed: %.2f MiB/s", bytesPerSecond / 1048576));
244
-                } else if (bytesPerSecond > 1024) {
245
-                    speed.setText(String.format("Speed: %.2f KiB/s", bytesPerSecond / 1024));
246
-                } else {
247
-                    speed.setText(String.format("Speed: %.2f B/s", bytesPerSecond));
248
-                }
249
-
250
-                remaining.setText(String.format("Time Remaining: %s",
251
-                        duration((int) container.getRemainingTime())));
252
-                taken.setText(String.format("Time Taken: %s", container.getStartTime() == 0
253
-                        ? "N/A" : duration(container.getElapsedTime())));
254
-            }
255
-        });
256
-    }
257
-
258
-    /**
259
-     * Get the duration in seconds as a string.
260
-     *
261
-     * @param secondsInput to get duration for
262
-     * @return Duration as a string
263
-     */
264
-    private String duration(final long secondsInput) {
265
-        final StringBuilder result = new StringBuilder();
266
-        final long hours = (secondsInput / 3600);
267
-        final long minutes = (secondsInput / 60 % 60);
268
-        final long seconds = (secondsInput % 60);
269
-
270
-        if (hours > 0) {
271
-            result.append(hours + ":");
272
-        }
273
-        result.append(String.format("%0,2d:%0,2d", minutes, seconds));
274
-
275
-        return result.toString();
276
-    }
277
-
278 49
 }

+ 41
- 31
src/com/dmdirc/addons/ui_swing/SwingWindowFactory.java View File

@@ -34,12 +34,13 @@ import com.dmdirc.ui.core.components.WindowComponent;
34 34
 import com.dmdirc.ui.interfaces.FrameListener;
35 35
 import com.dmdirc.ui.interfaces.Window;
36 36
 import com.dmdirc.util.ListenerList;
37
+
37 38
 import java.util.Arrays;
38 39
 import java.util.Collection;
39
-
40 40
 import java.util.HashMap;
41 41
 import java.util.HashSet;
42 42
 import java.util.Map;
43
+import java.util.Set;
43 44
 
44 45
 /**
45 46
  * Handles creation of windows in the Swing UI.
@@ -49,26 +50,38 @@ import java.util.Map;
49 50
 public class SwingWindowFactory implements FrameListener {
50 51
 
51 52
     /** A map of known implementations of window interfaces. */
52
-    private static final Map<Collection<String>, Class<? extends Window>> IMPLEMENTATIONS
53
-            = new HashMap<Collection<String>, Class<? extends Window>>();
54
-
53
+    private final Map<Collection<String>, Class<? extends Window>>
54
+            implementations = new HashMap<Collection<String>,
55
+            Class<? extends Window>>();
55 56
     /** A map of frame containers to their Swing windows. */
56
-    private final Map<FrameContainer, TextFrame> windows = new HashMap<FrameContainer, TextFrame>();
57
+    private final Map<FrameContainer, TextFrame> windows
58
+            = new HashMap<FrameContainer, TextFrame>();
59
+    /** The controller that owns this window factory. */
60
+    private final SwingController controller;
61
+    /** Our list of listeners. */
62
+    private final ListenerList listeners = new ListenerList();
63
+
64
+    /**
65
+     * Creates a new window factory for the specified controller.
66
+     *
67
+     * @param controller The controller this factory is for
68
+     */
69
+    public SwingWindowFactory(final SwingController controller) {
70
+        this.controller = controller;
57 71
 
58
-    static {
59
-        IMPLEMENTATIONS.put(new HashSet<String>(
72
+        registerImplementation(new HashSet<String>(
60 73
                 Arrays.asList(WindowComponent.TEXTAREA.getIdentifier())),
61 74
                 CustomFrame.class);
62
-        IMPLEMENTATIONS.put(new HashSet<String>(
75
+        registerImplementation(new HashSet<String>(
63 76
                 Arrays.asList(WindowComponent.TEXTAREA.getIdentifier(),
64 77
                 WindowComponent.INPUTFIELD.getIdentifier())),
65 78
                 CustomInputFrame.class);
66
-        IMPLEMENTATIONS.put(new HashSet<String>(
79
+        registerImplementation(new HashSet<String>(
67 80
                 Arrays.asList(WindowComponent.TEXTAREA.getIdentifier(),
68 81
                 WindowComponent.INPUTFIELD.getIdentifier(),
69 82
                 WindowComponent.CERTIFICATE_VIEWER.getIdentifier())),
70 83
                 ServerFrame.class);
71
-        IMPLEMENTATIONS.put(new HashSet<String>(
84
+        registerImplementation(new HashSet<String>(
72 85
                 Arrays.asList(WindowComponent.TEXTAREA.getIdentifier(),
73 86
                 WindowComponent.INPUTFIELD.getIdentifier(),
74 87
                 WindowComponent.TOPICBAR.getIdentifier(),
@@ -76,19 +89,9 @@ public class SwingWindowFactory implements FrameListener {
76 89
                 ChannelFrame.class);
77 90
     }
78 91
 
79
-    /** The controller that owns this window factory. */
80
-    private final SwingController controller;
81
-
82
-    /** Our list of listeners. */
83
-    private final ListenerList listeners = new ListenerList();
84
-
85
-    /**
86
-     * Creates a new window factory for the specified controller.
87
-     *
88
-     * @param controller The controller this factory is for
89
-     */
90
-    public SwingWindowFactory(final SwingController controller) {
91
-        this.controller = controller;
92
+    public final void registerImplementation(final Set<String> components,
93
+            final Class<? extends Window> clazz) {
94
+        implementations.put(components, clazz);
92 95
     }
93 96
 
94 97
     /**
@@ -128,14 +131,17 @@ public class SwingWindowFactory implements FrameListener {
128 131
             final boolean focus) {
129 132
         final Class<? extends Window> clazz;
130 133
 
131
-        if (IMPLEMENTATIONS.containsKey(window.getComponents())) {
132
-            clazz = IMPLEMENTATIONS.get(window.getComponents());
134
+        if (implementations.containsKey(window.getComponents())) {
135
+            clazz = implementations.get(window.getComponents());
133 136
         } else {
134
-            clazz = window.getWindowClass();
137
+            Logger.userError(ErrorLevel.HIGH,
138
+                    "Unable to create window: Unknown type");
139
+            return null;
135 140
         }
136 141
 
137 142
         try {
138
-            final TextFrame frame = (TextFrame) clazz.getConstructors()[0].newInstance(controller, window);
143
+            final TextFrame frame = (TextFrame) clazz.getConstructors()[0]
144
+                    .newInstance(controller, window);
139 145
             windows.put(window, frame);
140 146
 
141 147
             return frame;
@@ -147,7 +153,8 @@ public class SwingWindowFactory implements FrameListener {
147 153
 
148 154
     /**
149 155
      * Retrieves a single Swing UI created window belonging to the specified
150
-     * container. Returns null if the container is null or no such window exists.
156
+     * container. Returns null if the container is null or no such window
157
+     * exists.
151 158
      *
152 159
      * @param window The container whose windows should be searched
153 160
      * @return A relevant window or null
@@ -177,7 +184,8 @@ public class SwingWindowFactory implements FrameListener {
177 184
                     return;
178 185
                 }
179 186
 
180
-                for (SwingWindowListener listener : listeners.get(SwingWindowListener.class)) {
187
+                for (SwingWindowListener listener : listeners.get(
188
+                        SwingWindowListener.class)) {
181 189
                     listener.windowAdded(parentWindow, childWindow);
182 190
                 }
183 191
 
@@ -190,7 +198,8 @@ public class SwingWindowFactory implements FrameListener {
190 198
 
191 199
     /** {@inheritDoc} */
192 200
     @Override
193
-    public void delWindow(final FrameContainer parent, final FrameContainer window) {
201
+    public void delWindow(final FrameContainer parent,
202
+            final FrameContainer window) {
194 203
         UIUtilities.invokeLater(new Runnable() {
195 204
 
196 205
             @Override
@@ -198,7 +207,8 @@ public class SwingWindowFactory implements FrameListener {
198 207
                 final TextFrame parentWindow = getSwingWindow(parent);
199 208
                 final TextFrame childWindow = getSwingWindow(window);
200 209
 
201
-                for (SwingWindowListener listener : listeners.get(SwingWindowListener.class)) {
210
+                for (SwingWindowListener listener : listeners.get(
211
+                        SwingWindowListener.class)) {
202 212
                     listener.windowDeleted(parentWindow, childWindow);
203 213
                 }
204 214
 

+ 95
- 0
src/com/dmdirc/addons/ui_swing/components/frames/ComponentFrame.java View File

@@ -0,0 +1,95 @@
1
+/*
2
+ * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+package com.dmdirc.addons.ui_swing.components.frames;
23
+
24
+import com.dmdirc.FrameContainer;
25
+import com.dmdirc.addons.ui_swing.SwingController;
26
+import com.dmdirc.commandparser.PopupType;
27
+
28
+import javax.swing.JPopupMenu;
29
+
30
+import net.miginfocom.swing.MigLayout;
31
+
32
+/**
33
+ * A very basic frame that adds components from a frame container.
34
+ */
35
+public class ComponentFrame extends TextFrame {
36
+
37
+    /**
38
+     * A version number for this class. It should be changed whenever the class
39
+     * structure is changed (or anything else that would prevent serialized
40
+     * objects being unserialized with the new class).
41
+     */
42
+    private static final long serialVersionUID = 2;
43
+
44
+    /**
45
+     * Creates a new instance of CustomFrame.
46
+     *
47
+     * @param owner The frame container that owns this frame
48
+     * @param controller Swing controller
49
+     */
50
+    public ComponentFrame(final SwingController controller,
51
+            final FrameContainer owner) {
52
+        super(owner, controller);
53
+
54
+        initComponents();
55
+    }
56
+
57
+    /**
58
+     * Initialises components in this frame.
59
+     */
60
+    private void initComponents() {
61
+        setLayout(new MigLayout("ins 0, fill, hidemode 3, wrap 1"));
62
+        add(getTextPane(), "grow, push");
63
+        add(getSearchBar(), "growx, pushx");
64
+    }
65
+
66
+    /** {@inheritDoc} */
67
+    @Override
68
+    public PopupType getNicknamePopupType() {
69
+        return null;
70
+    }
71
+
72
+    /** {@inheritDoc} */
73
+    @Override
74
+    public PopupType getChannelPopupType() {
75
+        return null;
76
+    }
77
+
78
+    /** {@inheritDoc} */
79
+    @Override
80
+    public PopupType getHyperlinkPopupType() {
81
+        return null;
82
+    }
83
+
84
+    /** {@inheritDoc} */
85
+    @Override
86
+    public PopupType getNormalPopupType() {
87
+        return null;
88
+    }
89
+
90
+    /** {@inheritDoc} */
91
+    @Override
92
+    public void addCustomPopupItems(final JPopupMenu popupMenu) {
93
+        //Add no custom popup items
94
+    }
95
+}

+ 102
- 0
src/com/dmdirc/addons/ui_swing/components/frames/ComponentInputFrame.java View File

@@ -0,0 +1,102 @@
1
+/*
2
+ * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.addons.ui_swing.components.frames;
24
+
25
+import com.dmdirc.WritableFrameContainer;
26
+import com.dmdirc.addons.ui_swing.SwingController;
27
+import com.dmdirc.addons.ui_swing.components.inputfields.SwingInputHandler;
28
+import com.dmdirc.commandparser.PopupType;
29
+
30
+import javax.swing.JPopupMenu;
31
+
32
+import net.miginfocom.swing.MigLayout;
33
+
34
+/**
35
+ * A component frame that includes an input field (for use with writable
36
+ * containers).
37
+ */
38
+public class ComponentInputFrame extends InputTextFrame {
39
+
40
+    /**
41
+     * A version number for this class. It should be changed whenever the class
42
+     * structure is changed (or anything else that would prevent serialized
43
+     * objects being unserialized with the new class).
44
+     */
45
+    private static final long serialVersionUID = 2;
46
+
47
+    /**
48
+     * Creates a new instance of CustomInputFrame.
49
+     *
50
+     * @param owner The frame container that owns this frame
51
+     * @param controller Swing controller
52
+     */
53
+    public ComponentInputFrame(final SwingController controller,
54
+            final WritableFrameContainer owner) {
55
+        super(controller, owner);
56
+
57
+        setInputHandler(new SwingInputHandler(getInputField(),
58
+                owner.getCommandParser(), getContainer()));
59
+
60
+        initComponents();
61
+    }
62
+
63
+    /**
64
+     * Initialises components in this frame.
65
+     */
66
+    private void initComponents() {
67
+        setLayout(new MigLayout("ins 0, fill, hidemode 3, wrap 1"));
68
+        add(getTextPane(), "grow, push");
69
+        add(getSearchBar(), "growx, pushx");
70
+        add(inputPanel, "growx, pushx");
71
+    }
72
+
73
+    /** {@inheritDoc} */
74
+    @Override
75
+    public PopupType getNicknamePopupType() {
76
+        return null;
77
+    }
78
+
79
+    /** {@inheritDoc} */
80
+    @Override
81
+    public PopupType getChannelPopupType() {
82
+        return null;
83
+    }
84
+
85
+    /** {@inheritDoc} */
86
+    @Override
87
+    public PopupType getHyperlinkPopupType() {
88
+        return null;
89
+    }
90
+
91
+    /** {@inheritDoc} */
92
+    @Override
93
+    public PopupType getNormalPopupType() {
94
+        return null;
95
+    }
96
+
97
+    /** {@inheritDoc} */
98
+    @Override
99
+    public void addCustomPopupItems(final JPopupMenu popupMenu) {
100
+        //Add no custom popup items
101
+    }
102
+}

Loading…
Cancel
Save