scratchpadplugin.java
来自「开源项目openfire的完整源程序」· Java 代码 · 共 508 行 · 第 1/2 页
JAVA
508 行
/** * $Revision: $ * $Date: $ * * Copyright (C) 2006 Jive Software. All rights reserved. * * This software is published under the terms of the GNU Lesser Public License (LGPL), * a copy of which is included in this distribution. */package org.jivesoftware.sparkimpl.plugin.scratchpad;import org.jdesktop.swingx.calendar.DateUtils;import org.jivesoftware.resource.SparkRes;import org.jivesoftware.resource.Res;import org.jivesoftware.spark.SparkManager;import org.jivesoftware.spark.component.RolloverButton;import org.jivesoftware.spark.component.VerticalFlowLayout;import org.jivesoftware.spark.plugin.Plugin;import org.jivesoftware.spark.ui.ContactList;import org.jivesoftware.spark.util.GraphicUtils;import org.jivesoftware.spark.util.ModelUtil;import org.jivesoftware.spark.util.ResourceUtils;import org.jivesoftware.spark.util.SwingWorker;import javax.swing.AbstractAction;import javax.swing.Action;import javax.swing.BorderFactory;import javax.swing.ButtonGroup;import javax.swing.JButton;import javax.swing.JComponent;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextField;import javax.swing.JTextPane;import javax.swing.JToggleButton;import javax.swing.KeyStroke;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Font;import java.awt.GradientPaint;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.Insets;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseMotionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.Iterator;import java.util.List;/** * */public class ScratchPadPlugin implements Plugin { private ContactList contactList; public static boolean SHOW_ALL_TASKS = true; private SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy"); public void initialize() { contactList = SparkManager.getWorkspace().getContactList(); contactList.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("control F6"), "viewNotes"); contactList.getActionMap().put("viewNotes", new AbstractAction("viewNotes") { public void actionPerformed(ActionEvent evt) { // Retrieve notes and dispaly in editor. retrieveNotes(); } }); contactList.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("control F5"), "viewTasks"); contactList.getActionMap().put("viewTasks", new AbstractAction("viewTasks") { public void actionPerformed(ActionEvent evt) { // Retrieve notes and dispaly in editor. showTaskList(); } }); int index = -1; JPanel commandPanel = SparkManager.getWorkspace().getCommandPanel(); for (int i = 0; i < commandPanel.getComponentCount(); i++) { if (commandPanel.getComponent(i) instanceof JLabel) { index = i; break; } } RolloverButton taskButton = new RolloverButton(SparkRes.getImageIcon(SparkRes.DESKTOP_IMAGE)); RolloverButton notesButton = new RolloverButton(SparkRes.getImageIcon(SparkRes.DOCUMENT_16x16)); taskButton.setToolTipText("View Task List"); notesButton.setToolTipText("View Notes"); taskButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { showTaskList(); } }); notesButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { retrieveNotes(); } }); // Add To toolbar SparkManager.getWorkspace().getCommandPanel().add(taskButton, index); SparkManager.getWorkspace().getCommandPanel().add(notesButton, index); SparkManager.getWorkspace().getCommandPanel().validate(); SparkManager.getWorkspace().getCommandPanel().invalidate(); SparkManager.getWorkspace().getCommandPanel().repaint(); // Start notifications. new TaskNotification(); } private void showTaskList() { final JFrame frame = new JFrame("Tasks"); frame.setIconImage(SparkManager.getMainWindow().getIconImage()); final List<TaskUI> taskList = new ArrayList<TaskUI>(); final JPanel mainPanel = new JPanel(); mainPanel.setLayout(new VerticalFlowLayout(VerticalFlowLayout.TOP, 0, 0, true, false)); mainPanel.setBackground(Color.white); final JPanel topPanel = new JPanel(new GridBagLayout()); final JTextField taskField = new JTextField(); final JTextField dueDateField = new JTextField(); final JButton addButton = new JButton("Add"); final JLabel addTaskLabel = new JLabel("Add Task"); topPanel.setOpaque(false); topPanel.add(addTaskLabel, new GridBagConstraints(0, 0, 1, 1, .9, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0)); topPanel.add(taskField, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 0, 2), 0, 0)); topPanel.add(dueDateField, new GridBagConstraints(1, 1, 1, 1, 0.1, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 0, 2), 50, 0)); topPanel.add(addButton, new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 2, 0, 2), 0, 0)); topPanel.add(new JLabel("Use mm/dd/yy"), new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0)); mainPanel.add(topPanel); // Add Selection final JPanel middlePanel = new JPanel(new GridBagLayout()); final JLabel showLabel = new JLabel("Show:"); final JToggleButton allButton = new JToggleButton("All"); final JToggleButton activeButton = new JToggleButton("Active"); final ButtonGroup buttonGroup = new ButtonGroup(); buttonGroup.add(allButton); buttonGroup.add(activeButton); middlePanel.setOpaque(false); middlePanel.add(showLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0)); middlePanel.add(allButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0)); middlePanel.add(activeButton, new GridBagConstraints(2, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0)); mainPanel.add(middlePanel); mainPanel.setBackground(Color.white); final JPanel titlePanel = new JPanel(new BorderLayout()) { public void paintComponent(Graphics g) { Color startColor = Color.white; Color endColor = new Color(198, 211, 247); Graphics2D g2 = (Graphics2D)g; int w = getWidth(); int h = getHeight(); GradientPaint gradient = new GradientPaint(0, 0, startColor, w, h, endColor, true); g2.setPaint(gradient); g2.fillRect(0, 0, w, h); } }; final JLabel taskLabel = new JLabel("Due "); taskLabel.setFont(taskLabel.getFont().deriveFont(Font.BOLD)); titlePanel.add(taskLabel, BorderLayout.EAST); mainPanel.add(titlePanel); Action showAllAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { for (TaskUI ui : taskList) { ui.setVisible(true); } SHOW_ALL_TASKS = true; } }; Action showActiveAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { for (TaskUI ui : taskList) { if (ui.isSelected()) { ui.setVisible(false); } } SHOW_ALL_TASKS = false; } }; final Action addAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { String taskTitle = taskField.getText(); if (!ModelUtil.hasLength(taskTitle)) { return; } Task task = new Task(); task.setTitle(taskTitle); // Set creation time. final Date creationDate = new Date(); task.setCreatedDate(creationDate.getTime()); // Set due date. String dueDate = dueDateField.getText(); if (ModelUtil.hasLength(dueDate)) { try { Date date = formatter.parse(dueDate); task.setDueDate(date.getTime()); } catch (ParseException e1) { } } taskField.setText(""); final TaskUI taskUI = new TaskUI(task); mainPanel.add(taskUI); taskList.add(taskUI);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?