📄 documenteditorview.java
字号:
/* * Copyright (c) 2007, Sun Microsystems, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the distribution. * * Neither the name of Sun Microsystems, Inc. nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */package documenteditor;import java.awt.AWTException;import java.awt.Image;import java.awt.MenuItem;import java.awt.Point;import java.awt.PopupMenu;import java.awt.SystemTray;import java.awt.Toolkit;import java.awt.TrayIcon;import javax.swing.event.DocumentEvent;import org.jdesktop.application.Action;import org.jdesktop.application.ResourceMap;import org.jdesktop.application.SingleFrameApplication;import org.jdesktop.application.FrameView;import org.jdesktop.application.TaskMonitor;import org.jdesktop.application.Task;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.io.File;import java.util.Date;import java.util.EventObject;import java.util.logging.Level;import java.util.logging.Logger;import javax.swing.Timer;import javax.swing.Icon;import javax.swing.JDialog;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.event.DocumentListener;import org.jdesktop.application.Application;import javax.swing.filechooser.FileFilter;import javax.swing.tree.DefaultMutableTreeNode;import javax.swing.tree.DefaultTreeModel;/** * This application is a simple text editor. This class displays the main frame * of the application and provides much of the logic. This class is called by * the main application class, DocumentEditorApp. For an overview of the * application see the comments for the DocumentEditorApp class. */public class DocumentEditorView extends FrameView { private File file = new File("untitled.txt"); private boolean modified = false; private DefaultMutableTreeNode OnlineNode = null; private DefaultMutableTreeNode OfflineNode = null; private FixDataDialog dialog = null; private SetChannelJDialog setChannelJDialog = null; private TrayIcon trayIcon = null; private DefaultTreeModel SiteTreeModel = null; public DefaultMutableTreeNode getOfflineNode() { return OfflineNode; } public DefaultMutableTreeNode getOnlineNode() { return OnlineNode; } public javax.swing.JTree getJTree1() { return jTree1; } //更换图标 public void SetIco(Image ico){ getFrame().setIconImage(ico); trayIcon.setImage(ico); } //添加登录站点 public void AddOnlineSite(DefaultMutableTreeNode tsn){ GPRSClientThread thread = (GPRSClientThread) tsn.getUserObject(); int j = -1; for(int i=0;i<OnlineNode.getChildCount();i++){ DefaultMutableTreeNode temp_node = (DefaultMutableTreeNode)OnlineNode.getChildAt(i); GPRSClientThread gct = (GPRSClientThread)temp_node.getUserObject(); if(gct.getID().toString().equals(thread.getID().toString())){ OnlineNode.remove(i); } } for(int i=0;i<OnlineNode.getChildCount();i++){ DefaultMutableTreeNode temp_node = (DefaultMutableTreeNode)OnlineNode.getChildAt(i); GPRSClientThread gct = (GPRSClientThread)temp_node.getUserObject(); if(gct.toString().compareTo(thread.toString())>0){ j = i; OnlineNode.insert(tsn, j); break; } } if(j==-1){ OnlineNode.add(tsn); } for(int i=0;i<getOfflineNode().getChildCount();i++){ DefaultMutableTreeNode temp_node = (DefaultMutableTreeNode)getOfflineNode().getChildAt(i); GPRSClientThread gct = (GPRSClientThread)temp_node.getUserObject(); if(gct.getID().toString().equals(thread.getID().toString())){ getOfflineNode().remove(i); } } } //添加信息 public void AddMessage(String Str){ if(Str.length()>1){ if(Str.substring(0,1).equals("#")){ int i1 = Str.indexOf("("); int i2 = Str.indexOf(")"); String ds = Str.substring(i1+1,i2); for(int i=0;i<OnlineNode.getChildCount();i++){ DefaultMutableTreeNode temp_node = (DefaultMutableTreeNode)OnlineNode.getChildAt(i); GPRSClientThread gct = (GPRSClientThread)temp_node.getUserObject(); String IPPORT = gct.getInetAddress()+":"+gct.getPort(); if(IPPORT.equals(ds)){ OnlineNode.remove(i); getOfflineNode().add(temp_node); } } //jTree1.updateUI(); SiteTreeModel.nodeStructureChanged(OnlineNode); SiteTreeModel.nodeStructureChanged(OfflineNode); jTree1.expandRow(1); }else if(Str.substring(0,1).equals("$")){ SiteTreeModel.nodeStructureChanged(OnlineNode); SiteTreeModel.nodeStructureChanged(OfflineNode); jTree1.expandRow(1); //jTree1.updateUI(); } } if(textArea.getLineCount()>1000){ textArea.setText(""); } textArea.append(Str+"\r\n"); textArea.setCaretPosition(textArea.getText().length()); } public DocumentEditorView(SingleFrameApplication app) { super(app); // generated GUI builder code initComponents(); //初始化托盘 if (SystemTray.isSupported()){ try { SystemTray tray = SystemTray.getSystemTray(); Image image = Toolkit.getDefaultToolkit().getImage(getClass().getResource("main.gif")); getFrame().setIconImage(image); PopupMenu popup = new PopupMenu(); MenuItem item = new MenuItem("显示主窗体"); item.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { getFrame().setVisible(true); } }); popup.add(item); trayIcon = new TrayIcon(image, "GPRS无线数据传输系统4.1", popup); trayIcon.setImageAutoSize(true); tray.add(trayIcon); trayIcon.addMouseListener(new MouseListener() { public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) { getFrame().setVisible(true); } } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } }); } catch (AWTException ex) { Logger.getLogger(DocumentEditorView.class.getName()).log(Level.SEVERE, null, ex); } } //关闭事件定义 getFrame().setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); //建立取历史数据对话框 dialog = new FixDataDialog(getFrame(), true); dialog.setTitle("输入..."); //建立通道设置对话框 setChannelJDialog = new SetChannelJDialog(getFrame(), true); setChannelJDialog.setTitle("通道设置"); //setChannelJDialog.setVisible(true); //初始化树形目录 DefaultMutableTreeNode GPRSRootNode = new DefaultMutableTreeNode("GRPSModem"); SiteTreeModel = new DefaultTreeModel(GPRSRootNode); jTree1.setModel(SiteTreeModel); OnlineNode = new DefaultMutableTreeNode("Online"); OfflineNode = new DefaultMutableTreeNode("Offline"); GPRSRootNode.add(OnlineNode); GPRSRootNode.add(OfflineNode); jTree1.expandRow(0); // status bar initialization - message timeout, idle icon and busy animation, etc ResourceMap resourceMap = getResourceMap(); int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout"); messageTimer = new Timer(messageTimeout, new ActionListener() { public void actionPerformed(ActionEvent e) { statusMessageLabel.setText(""); } }); messageTimer.setRepeats(false); int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate"); for (int i = 0; i < busyIcons.length; i++) { busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]"); } busyIconTimer = new Timer(busyAnimationRate, new ActionListener() { public void actionPerformed(ActionEvent e) { busyIconIndex = (busyIconIndex + 1) % busyIcons.length; statusAnimationLabel.setIcon(busyIcons[busyIconIndex]); } }); idleIcon = resourceMap.getIcon("StatusBar.idleIcon"); statusAnimationLabel.setIcon(idleIcon); progressBar.setVisible(false); // connect action tasks to status bar via TaskMonitor TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext()); taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() { public void propertyChange(java.beans.PropertyChangeEvent evt) { String propertyName = evt.getPropertyName(); if ("started".equals(propertyName)) { if (!busyIconTimer.isRunning()) { statusAnimationLabel.setIcon(busyIcons[0]); busyIconIndex = 0; busyIconTimer.start(); } progressBar.setVisible(true); progressBar.setIndeterminate(true); } else if ("done".equals(propertyName)) { busyIconTimer.stop(); statusAnimationLabel.setIcon(idleIcon); progressBar.setVisible(false); progressBar.setValue(0); } else if ("message".equals(propertyName)) { String text = (String)(evt.getNewValue()); statusMessageLabel.setText((text == null) ? "" : text); messageTimer.restart(); } else if ("progress".equals(propertyName)) { int value = (Integer)(evt.getNewValue()); progressBar.setVisible(true); progressBar.setIndeterminate(false); progressBar.setValue(value); } } }); // if the document is ever edited, assume that it needs to be saved textArea.getDocument().addDocumentListener(new DocumentListener() { public void changedUpdate(DocumentEvent e) { setModified(true); } public void insertUpdate(DocumentEvent e) { setModified(true); } public void removeUpdate(DocumentEvent e) { setModified(true); } }); // ask for confirmation on exit getApplication().addExitListener(new ConfirmExit()); } /** * The File currently being edited. The default value of this * property is "untitled.txt". * <p> * This is a bound read-only property. It is never null. * * @return the value of the file property. * @see #isModified */ public File getFile() { return file; } /* Set the bound file property and update the GUI.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -