📄 timelinedisplayer.java
字号:
/* * Copyright (c) 2003, KNOPFLERFISH project * 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 the KNOPFLERFISH project 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 org.knopflerfish.bundle.desktop.swing;import org.osgi.framework.*;import org.knopflerfish.service.desktop.*;import javax.swing.*;import javax.swing.event.*;import java.awt.*;import java.awt.event.*;import java.util.*;public class TimeLineDisplayer extends DefaultSwingBundleDisplayer { ListSelectionModel model; Desktop desktop; // Long (time) -> BundleEvent SortedMap bundleEvents = new TreeMap(); // Bundle -> (Long (time) -> ServiceEvent) Map bundleToServiceEvents = new HashMap(); public TimeLineDisplayer(BundleContext bc) { super(bc, "Time line", "Time line of bundle events", false); desktop = Activator.desktop; } public void bundleChanged(BundleEvent ev) { super.bundleChanged(ev); Bundle b = ev.getBundle(); FWEvent fwe = new FWEvent(b, ev.getType()); bundleEvents.put(new Long(fwe.getId()), fwe); repaintComponents(); } public void serviceChanged(ServiceEvent ev) { ServiceReference sr = ev.getServiceReference(); FWEvent fwe = new FWEvent(sr, ev.getType()); Map serviceEvents = (Map)bundleToServiceEvents.get(sr.getBundle()); if(serviceEvents == null) { serviceEvents = new TreeMap(); bundleToServiceEvents.put(sr.getBundle(), serviceEvents); } serviceEvents.put(new Long(fwe.getId()), fwe); repaintComponents(); } void clear() { bundleEvents.clear(); bundleToServiceEvents.clear(); } public JComponent newJComponent() { JTimeLine tl = new JTimeLine(); tl.reloadAll(false); return tl; } JScrollPane scroll; class JTimeLine extends JPanel { JBundleGraph graph; public JTimeLine() { setLayout(new BorderLayout()); setBackground(Color.white); graph = new JBundleGraph(); scroll = new JScrollPane(graph); add(scroll, BorderLayout.CENTER); JToolBar cmdPanel = new JToolBar(JToolBar.VERTICAL); cmdPanel.setFloatable(false); cmdPanel.add(new JButton() { { setIcon(desktop.reloadIcon); setToolTipText("Reload events"); addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { reloadAll(true); } }); }}); cmdPanel.add(new JToolBar.Separator()); cmdPanel.add(new JButton() { { setIcon(desktop.magPlusIcon); setToolTipText("Zoom in"); addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { Point p = scroll.getViewport().getViewPosition(); graph.doZoom((int)(p.x * zoomK), (int)(p.y * zoomK), zoomK, zoomK); } }); }}); cmdPanel.add(new JButton() { { setIcon(desktop.magMinusIcon); setToolTipText("Zoom out"); addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { Point p = scroll.getViewport().getViewPosition(); graph.doZoom((int)(p.x / zoomK), (int)(p.y / zoomK), 1/zoomK, 1/zoomK); } }); }}); cmdPanel.add(new JButton() { { setIcon(desktop.magFitIcon); setToolTipText("Zoom out all"); addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { graph.doZoomOutAll(); } }); }}); add(cmdPanel, BorderLayout.WEST); } void reloadAll(boolean bAsk) { int n = 0; if(bAsk) { Object[] options = {Strings.get("yes"), Strings.get("cancel")}; n =JOptionPane .showOptionDialog(Activator.desktop.frame, Strings.get("This will clear all old events and reload with current framework status"), Strings.get("Reload all events?"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[1]); } if(n == 0) { clear(); getAllBundles(); getAllServices(); invalidate(); } } double zoomK = 1.1; // set any currently hilited bundle by in paintBundles() Bundle hiliteBundle = null; int hiliteBundleIx = -1; // The component actually drawing the time line class JBundleGraph extends JPanel { double zoomFac = 1.0; int mouseX = 0; int mouseY = 0; int mouseDragX = 0; int mouseDragY = 0; boolean bIsDragging = false; public JBundleGraph() { addMouseMotionListener(new MouseMotionListener() { public void mouseMoved(MouseEvent ev) { saveMousePos(ev); repaint(); } public void mouseDragged(MouseEvent ev) { bIsDragging = true; mouseDragX = ev.getX(); mouseDragY = ev.getY(); repaint(); } }); addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent ev) { saveMousePos(ev); } public void mouseReleased(MouseEvent ev) { bIsDragging = false; mouseDragX = ev.getX(); mouseDragY = ev.getY(); zoomTo(mouseX, mouseY, mouseDragX, mouseDragY); repaint(); } public void mouseClicked(MouseEvent ev) { saveMousePos(ev); double k = zoomK; if((ev.getModifiers() & MouseEvent.BUTTON1_MASK) != 0) { bundleSelModel.clearSelection(); if(hiliteBundle != null) { bundleSelModel.setSelected(hiliteBundle.getBundleId(), true); } } else { double kx = 1.0; double ky = 1.0; if((ev.getModifiers() & MouseEvent.ALT_MASK) != 0) { kx = k; } if((ev.getModifiers() & MouseEvent.CTRL_MASK) != 0) { ky = k; } if((ev.getModifiers() & MouseEvent.SHIFT_MASK) != 0) { kx = 1/ kx; ky = 1/ ky; } doZoom(ev.getX(), ev.getY(), kx, ky); } } }); } void saveMousePos(MouseEvent ev) { mouseX = ev.getX(); mouseY = ev.getY(); } void doZoom(int left, int top, double kx, double ky) { JViewport viewPort = scroll.getViewport(); Dimension size = getSize(); Dimension viewSize = viewPort.getExtentSize(); double w = Math.min(10000, size.width * kx); double h = Math.min(10000, size.height * ky); Dimension newSize = new Dimension((int)w, (int)h); Rectangle rect = new Rectangle(left, top, viewSize.width, viewSize.height); setPreferredSize(newSize); revalidate(); scrollRectToVisible(rect); revalidate(); } void zoomTo(int x1, int y1, int x2, int y2) { int dx = Math.abs(x2 - x1); int dy = Math.abs(y2 - y1); if(dx <= 15 || dy <= 15) { return; } Dimension size = getSize(); double kx = (double)size.width / dx; double ky = (double)size.height / dy; doZoom((int)(x1 * kx), (int)(y1 * ky), kx, ky); } void doZoomOutAll() { JViewport viewPort = scroll.getViewport(); Dimension size = getSize(); Dimension newSize = viewPort.getExtentSize(); setPreferredSize(newSize); revalidate(); } protected void paintComponent(Graphics g) { try { Dimension size = getSize(); if(isOpaque()) { g.setColor(bgColor); g.fillRect(0,0,size.width, size.height); } paintAll(g); } catch (Exception e) { Activator.log.error("Failed to paint", e); } } public void paintAll(Graphics _g) { try { Graphics2D g = (Graphics2D)_g; Dimension size = getSize(); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); paintBundles(g); paintNearest(g); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); paintDrag(g); } catch (Exception e) { e.printStackTrace(); } } void paintDrag(Graphics2D g) { if(!bIsDragging) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -