Java tutorial
/* * Copyright (c) 2012 Alvin R. de Leon. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.piraso.ui.base; import org.piraso.io.impl.HttpEntrySource; import org.piraso.ui.api.NewContextMonitorModel; import org.apache.commons.lang.StringUtils; import org.openide.windows.TopComponent; import org.openide.windows.WindowManager; import javax.swing.*; import java.util.*; /** * * @author adeleon */ public class ConnectingDialog extends javax.swing.JDialog { private Map<NewContextMonitorModel, String> failures; private List<HttpEntrySource> validResults; private final List<NewContextMonitorModel> models; /** * Creates new form ConnectingDialog */ public ConnectingDialog(List<NewContextMonitorModel> models) { super(WindowManager.getDefault().getMainWindow(), true); this.models = models; failures = new HashMap<NewContextMonitorModel, String>(); validResults = new ArrayList<HttpEntrySource>(); initComponents(); getRootPane().setDoubleBuffered(false); setLocationRelativeTo(getOwner()); } public ConnectingDialog start() { new MyWorker().execute(); return this; } public boolean isOpened(String name) { Set<TopComponent> opened = TopComponent.getRegistry().getOpened(); for (TopComponent component : opened) { if (ContextMonitorTopComponent.class.isInstance(component)) { ContextMonitorTopComponent editor = (ContextMonitorTopComponent) component; if (StringUtils.equals(editor.getName(), name)) { return true; } } } return false; } public List<HttpEntrySource> getValidResults() { return validResults; } public Map<NewContextMonitorModel, String> getFailures() { return failures; } public void setLabel(String text) { lblStatus.setText(text); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { lblStatus = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE); setTitle(org.openide.util.NbBundle.getMessage(ConnectingDialog.class, "ConnectingDialog.title")); // NOI18N setResizable(false); lblStatus.setForeground(new java.awt.Color(0, 102, 0)); lblStatus.setIcon( new javax.swing.ImageIcon(getClass().getResource("/org/piraso/ui/base/icons/network.png"))); // NOI18N lblStatus.setText( org.openide.util.NbBundle.getMessage(ConnectingDialog.class, "ConnectingDialog.lblStatus.text")); // NOI18N org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(layout .createSequentialGroup().add(15, 15, 15).add(lblStatus).addContainerGap(126, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(layout .createSequentialGroup().add(14, 14, 14).add(lblStatus).addContainerGap(14, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JLabel lblStatus; // End of variables declaration//GEN-END:variables private class MyWorker extends SwingWorker { @Override protected Object doInBackground() throws Exception { while (!isVisible()) { try { Thread.sleep(300l); } catch (InterruptedException e) { } } for (NewContextMonitorModel m : models) { if (isOpened(m.getName())) { failures.put(m, "Already monitoring."); continue; } HttpEntrySource source = new HttpEntrySource(m.getPreferences(), m.getLoggingUrl(), m.getWatchedAddr()); source.setName(m.getName()); try { source.reset(); if (source.testConnection()) { validResults.add(source); } else { failures.put(m, "Connection Failure."); } } catch (Exception e) { failures.put(m, e.toString()); } } return null; } @Override protected void done() { dispose(); ContextMonitorDispatcher.handleResults(validResults, failures); } } }