001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 2005-2006 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.tcpmon.integration;
021:
022: import java.io.IOException;
023: import java.io.Serializable;
024: import java.net.URL;
025: import javax.swing.event.HyperlinkEvent;
026: import javax.swing.text.html.HTMLEditorKit;
027: import org.openide.ErrorManager;
028: import org.openide.awt.HtmlBrowser;
029: import org.openide.filesystems.FileObject;
030: import org.openide.filesystems.Repository;
031: import org.openide.util.NbBundle;
032: import org.openide.windows.TopComponent;
033: import org.openide.windows.WindowManager;
034: import tcpmon.TunnelConfig;
035:
036: /**
037: * Top component which displays something.
038: *
039: * @author Inderjeet Singh
040: * @author Lukas Jungmann
041: */
042: final class TcpMonitorTopComponent extends TopComponent {
043:
044: private static TcpMonitorTopComponent instance;
045: /** path to the icon used by the component and its open action */
046: static final String ICON_PATH = "org/netbeans/modules/tcpmon/integration/resources/menuitem.gif";
047:
048: private static final String PREFERRED_ID = "TcpMonitorTopComponent";
049:
050: private TcpMonitorTopComponent() {
051: initComponents();
052: setName(NbBundle.getMessage(TcpMonitorTopComponent.class,
053: "CTL_TCPMonitorTopComponent"));
054: setToolTipText(NbBundle.getMessage(
055: TcpMonitorTopComponent.class,
056: "HINT_TCPMonitorTopComponent"));
057: FileObject fo = Repository.getDefault().getDefaultFileSystem()
058: .findResource("tcpmon/readme.html");
059: try {
060: URL helpURL = fo.getURL();
061: tpInfo.setEditorKit(new HTMLEditorKit());
062: tpInfo.setEditable(false);
063: tpInfo.setPage(helpURL);
064: } catch (IOException ioe) {
065: ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL,
066: ioe.getLocalizedMessage());
067: }
068: // setIcon(Utilities.loadImage(ICON_PATH, true));
069: }
070:
071: /** This method is called from within the constructor to
072: * initialize the form.
073: * WARNING: Do NOT modify this code. The content of this method is
074: * always regenerated by the Form Editor.
075: */
076: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
077: private void initComponents() {
078: java.awt.GridBagConstraints gridBagConstraints;
079:
080: tabbedPane = new javax.swing.JTabbedPane();
081: adminPanel = new javax.swing.JPanel();
082: pConnection = new javax.swing.JPanel();
083: jLabel1 = new javax.swing.JLabel();
084: pConnectionInfo = new javax.swing.JPanel();
085: lLocalPort = new javax.swing.JLabel();
086: tfLocalPort = new javax.swing.JTextField();
087: lRemoteHost = new javax.swing.JLabel();
088: tfRemoteHost = new javax.swing.JTextField();
089: lRemotePort = new javax.swing.JLabel();
090: tfRemotePort = new javax.swing.JTextField();
091: bAddMonitor = new javax.swing.JButton();
092: pInfo = new javax.swing.JPanel();
093: spInfo = new javax.swing.JScrollPane();
094: tpInfo = new javax.swing.JEditorPane();
095:
096: adminPanel.setLayout(new java.awt.BorderLayout());
097:
098: adminPanel.setMinimumSize(new java.awt.Dimension(400, 400));
099: adminPanel.setPreferredSize(new java.awt.Dimension(400, 400));
100: org.openide.awt.Mnemonics.setLocalizedText(jLabel1,
101: org.openide.util.NbBundle.getMessage(
102: TcpMonitorTopComponent.class,
103: "LBL_Create_Connection"));
104:
105: pConnectionInfo.setLayout(new java.awt.GridBagLayout());
106:
107: pConnectionInfo.setMinimumSize(new java.awt.Dimension(150, 70));
108: pConnectionInfo
109: .setPreferredSize(new java.awt.Dimension(150, 80));
110: lLocalPort.setLabelFor(tfLocalPort);
111: org.openide.awt.Mnemonics
112: .setLocalizedText(lLocalPort, org.openide.util.NbBundle
113: .getMessage(TcpMonitorTopComponent.class,
114: "LBL_Local_Port"));
115: gridBagConstraints = new java.awt.GridBagConstraints();
116: gridBagConstraints.gridx = 0;
117: gridBagConstraints.gridy = 0;
118: gridBagConstraints.ipadx = 16;
119: gridBagConstraints.ipady = 9;
120: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
121: pConnectionInfo.add(lLocalPort, gridBagConstraints);
122:
123: tfLocalPort.setText("8080");
124: gridBagConstraints = new java.awt.GridBagConstraints();
125: gridBagConstraints.gridx = 1;
126: gridBagConstraints.gridy = 0;
127: gridBagConstraints.ipadx = 119;
128: gridBagConstraints.ipady = 4;
129: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
130: gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
131: pConnectionInfo.add(tfLocalPort, gridBagConstraints);
132:
133: lRemoteHost.setLabelFor(tfRemoteHost);
134: org.openide.awt.Mnemonics.setLocalizedText(lRemoteHost,
135: org.openide.util.NbBundle
136: .getMessage(TcpMonitorTopComponent.class,
137: "LBL_Server_Name"));
138: gridBagConstraints = new java.awt.GridBagConstraints();
139: gridBagConstraints.gridx = 0;
140: gridBagConstraints.gridy = 1;
141: gridBagConstraints.ipadx = 3;
142: gridBagConstraints.ipady = 9;
143: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
144: gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 0);
145: pConnectionInfo.add(lRemoteHost, gridBagConstraints);
146:
147: tfRemoteHost.setText("127.0.0.1");
148: gridBagConstraints = new java.awt.GridBagConstraints();
149: gridBagConstraints.gridx = 1;
150: gridBagConstraints.gridy = 1;
151: gridBagConstraints.gridheight = 2;
152: gridBagConstraints.ipadx = 119;
153: gridBagConstraints.ipady = 4;
154: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
155: gridBagConstraints.insets = new java.awt.Insets(6, 10, 0, 0);
156: pConnectionInfo.add(tfRemoteHost, gridBagConstraints);
157:
158: lRemotePort.setLabelFor(tfRemotePort);
159: org.openide.awt.Mnemonics.setLocalizedText(lRemotePort,
160: org.openide.util.NbBundle
161: .getMessage(TcpMonitorTopComponent.class,
162: "LBL_Server_Port"));
163: gridBagConstraints = new java.awt.GridBagConstraints();
164: gridBagConstraints.gridx = 0;
165: gridBagConstraints.gridy = 3;
166: gridBagConstraints.ipadx = 6;
167: gridBagConstraints.ipady = 9;
168: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
169: gridBagConstraints.insets = new java.awt.Insets(2, 0, 0, 0);
170: pConnectionInfo.add(lRemotePort, gridBagConstraints);
171:
172: tfRemotePort.setText("80");
173: gridBagConstraints = new java.awt.GridBagConstraints();
174: gridBagConstraints.gridx = 1;
175: gridBagConstraints.gridy = 3;
176: gridBagConstraints.gridheight = 2;
177: gridBagConstraints.ipadx = 119;
178: gridBagConstraints.ipady = 4;
179: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
180: gridBagConstraints.insets = new java.awt.Insets(6, 10, 16, 0);
181: pConnectionInfo.add(tfRemotePort, gridBagConstraints);
182:
183: org.openide.awt.Mnemonics.setLocalizedText(bAddMonitor,
184: org.openide.util.NbBundle
185: .getMessage(TcpMonitorTopComponent.class,
186: "BTN_Add_Monitor"));
187: bAddMonitor
188: .addActionListener(new java.awt.event.ActionListener() {
189: public void actionPerformed(
190: java.awt.event.ActionEvent evt) {
191: bAddMonitorActionPerformed(evt);
192: }
193: });
194:
195: org.jdesktop.layout.GroupLayout pConnectionLayout = new org.jdesktop.layout.GroupLayout(
196: pConnection);
197: pConnection.setLayout(pConnectionLayout);
198: pConnectionLayout
199: .setHorizontalGroup(pConnectionLayout
200: .createParallelGroup(
201: org.jdesktop.layout.GroupLayout.LEADING)
202: .add(
203: pConnectionLayout
204: .createSequentialGroup().add(
205: 20, 20, 20)
206: .add(jLabel1))
207: .add(
208: pConnectionLayout
209: .createSequentialGroup()
210: .add(30, 30, 30)
211: .add(
212: pConnectionInfo,
213: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
214: 240,
215: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
216: .add(
217: pConnectionLayout
218: .createSequentialGroup().add(
219: 90, 90, 90).add(
220: bAddMonitor)));
221: pConnectionLayout
222: .setVerticalGroup(pConnectionLayout
223: .createParallelGroup(
224: org.jdesktop.layout.GroupLayout.LEADING)
225: .add(
226: pConnectionLayout
227: .createSequentialGroup()
228: .add(20, 20, 20)
229: .add(jLabel1)
230: .add(15, 15, 15)
231: .add(
232: pConnectionInfo,
233: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
234: 110,
235: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
236: .add(bAddMonitor)));
237: adminPanel.add(pConnection, java.awt.BorderLayout.WEST);
238:
239: pInfo.setLayout(new java.awt.BorderLayout());
240:
241: tpInfo.setEditable(false);
242: tpInfo
243: .addHyperlinkListener(new javax.swing.event.HyperlinkListener() {
244: public void hyperlinkUpdate(
245: javax.swing.event.HyperlinkEvent evt) {
246: tpInfoHyperlinkUpdate(evt);
247: }
248: });
249:
250: spInfo.setViewportView(tpInfo);
251:
252: pInfo.add(spInfo, java.awt.BorderLayout.CENTER);
253:
254: adminPanel.add(pInfo, java.awt.BorderLayout.CENTER);
255:
256: tabbedPane.addTab(org.openide.util.NbBundle.getMessage(
257: TcpMonitorTopComponent.class, "TITLE_Admin"),
258: adminPanel);
259:
260: org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
261: this );
262: this .setLayout(layout);
263: layout.setHorizontalGroup(layout.createParallelGroup(
264: org.jdesktop.layout.GroupLayout.LEADING).add(
265: tabbedPane,
266: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 547,
267: Short.MAX_VALUE));
268: layout.setVerticalGroup(layout.createParallelGroup(
269: org.jdesktop.layout.GroupLayout.LEADING).add(
270: tabbedPane,
271: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 477,
272: Short.MAX_VALUE));
273: }// </editor-fold>//GEN-END:initComponents
274:
275: private void tpInfoHyperlinkUpdate(
276: javax.swing.event.HyperlinkEvent evt) {//GEN-FIRST:event_tpInfoHyperlinkUpdate
277: if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
278: try {
279: HtmlBrowser.URLDisplayer displayer = HtmlBrowser.URLDisplayer
280: .getDefault();
281: if (displayer != null) {
282: displayer.showURL(evt.getURL());
283: }
284: } catch (Exception e) {
285: //ignore
286: }
287: }
288: }//GEN-LAST:event_tpInfoHyperlinkUpdate
289:
290: private void bAddMonitorActionPerformed(
291: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAddMonitorActionPerformed
292: int serverPort = 0;
293: int localPort = 0;
294: try {
295: serverPort = Integer.parseInt(tfRemotePort.getText());
296: localPort = Integer.parseInt(tfLocalPort.getText());
297: } catch (Exception e) {
298: throw new RuntimeException(e);
299: }
300: addNewMonitorTab(tfRemoteHost.getText(), serverPort, localPort,
301: true, null);
302: }//GEN-LAST:event_bAddMonitorActionPerformed
303:
304: public void addNewMonitorTab(String serverHost, int serverPort,
305: int localPort, boolean startTunnel, String clientName) {
306: MonitorPanel p = new MonitorPanel();
307: TunnelConfig tc = new TunnelConfig(serverHost, serverPort,
308: localPort, startTunnel);
309: p.start(tc);
310: if (clientName == null) {
311: tabbedPane.addTab(NbBundle.getMessage(
312: TcpMonitorTopComponent.class, "TITLE_Port")
313: + " " + tc.localPort, p);
314: } else {
315: tabbedPane.addTab(clientName, null, p, clientName
316: + " (localhost:" + tc.localPort + " -> " + //NOI18N
317: serverHost + ":" + serverPort + ")"); //NOI18N
318: }
319: // Select the tab that just got created.
320: tabbedPane.setSelectedIndex(tabbedPane.indexOfComponent(p));
321: }
322:
323: // Variables declaration - do not modify//GEN-BEGIN:variables
324: private javax.swing.JPanel adminPanel;
325: private javax.swing.JButton bAddMonitor;
326: private javax.swing.JLabel jLabel1;
327: private javax.swing.JLabel lLocalPort;
328: private javax.swing.JLabel lRemoteHost;
329: private javax.swing.JLabel lRemotePort;
330: private javax.swing.JPanel pConnection;
331: private javax.swing.JPanel pConnectionInfo;
332: private javax.swing.JPanel pInfo;
333: private javax.swing.JScrollPane spInfo;
334: private javax.swing.JTabbedPane tabbedPane;
335: private javax.swing.JTextField tfLocalPort;
336: private javax.swing.JTextField tfRemoteHost;
337: private javax.swing.JTextField tfRemotePort;
338: private javax.swing.JEditorPane tpInfo;
339:
340: // End of variables declaration//GEN-END:variables
341:
342: /**
343: * Gets default instance. Do not use directly: reserved for *.settings files only,
344: * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
345: * To obtain the singleton instance, use {@link findInstance}.
346: */
347: public static synchronized TcpMonitorTopComponent getDefault() {
348: if (instance == null) {
349: instance = new TcpMonitorTopComponent();
350: }
351: return instance;
352: }
353:
354: /**
355: * Obtain the TcpMonitorTopComponent instance. Never call {@link #getDefault} directly!
356: */
357: public static synchronized TcpMonitorTopComponent findInstance() {
358: TopComponent win = WindowManager.getDefault().findTopComponent(
359: PREFERRED_ID);
360: if (win == null) {
361: ErrorManager
362: .getDefault()
363: .log(
364: ErrorManager.WARNING,
365: "Cannot find TCPMonitor component. It will not be located properly in the window system.");
366: return getDefault();
367: }
368: if (win instanceof TcpMonitorTopComponent) {
369: return (TcpMonitorTopComponent) win;
370: }
371: ErrorManager
372: .getDefault()
373: .log(
374: ErrorManager.WARNING,
375: "There seem to be multiple components with the '"
376: + PREFERRED_ID
377: + "' ID. That is a potential source of errors and unexpected behavior.");
378: return getDefault();
379: }
380:
381: public int getPersistenceType() {
382: return TopComponent.PERSISTENCE_ALWAYS;
383: }
384:
385: public void componentOpened() {
386: // TODO add custom code on component opening
387: }
388:
389: public void componentClosed() {
390: // TODO add custom code on component closing
391: }
392:
393: /** replaces this in object stream */
394: public Object writeReplace() {
395: return new ResolvableHelper();
396: }
397:
398: protected String preferredID() {
399: return PREFERRED_ID;
400: }
401:
402: final static class ResolvableHelper implements Serializable {
403: private static final long serialVersionUID = 1L;
404:
405: public Object readResolve() {
406: return TcpMonitorTopComponent.getDefault();
407: }
408: }
409:
410: }
|