📄 spin.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.fwspin;import org.osgi.framework.*;import org.osgi.service.packageadmin.*;import java.util.*;import java.awt.*;import java.awt.geom.*;import java.awt.event.*;import javax.swing.*;import org.knopflerfish.bundle.desktop.swing.Activator;/** * @author Erik Wistrand */public class Spin extends JPanel implements Runnable, BundleListener, ServiceListener { Thread runner = null; boolean bRun = false; long delay = 30; long targetTime = 1000 / 30; long updateTime = 0; long totalTime = 0; Dimension size = null; Image memImage = null; Graphics memG = null; boolean bShowFrameRate = false; boolean bShowBundleLegend = true; boolean use2D = true; boolean bShowBundleInfo = false; boolean bShowHelp = false; boolean bShowDeps = false; boolean bStepSize = false; double deltaA = Math.PI * 1.5; double aStep = Math.PI * 2 / 120; Map bundles = new TreeMap(); Map services = new HashMap(); Map active = new HashMap(); Object paintLock = services; double fontSize = 1.0; Object alphaHalf = null; String searchString = ""; boolean bSearchMode = false; Console console; public KeyListener kl = null; Color bgColor = new Color(20, 20, 80); Color textColor = bgColor.brighter().brighter().brighter(); public Spin() { super(); /* System.out.println("made spin"); addFocusListener(new FocusAdapter() { public void focusGained(FocusEvent e) { System.out.println("got focus"); } public void focusLost(FocusEvent e) { System.out.println("lost focus"); } }); */ Bundle[] bl = Activator.getTargetBC().getBundles(); for(int i = 0; bl != null && i < bl.length; i++) { bundleChanged(new BundleEvent(BundleEvent.INSTALLED, bl[i])); } Activator.getTargetBC().addBundleListener(this); try { ServiceReference [] srl = Activator.getTargetBC().getServiceReferences(null, null); for(int i = 0; srl != null && i < srl.length; i++) { serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, srl[i])); } Activator.getTargetBC().addServiceListener(this, null); } catch (Exception e) { e.printStackTrace(); } initFonts(); addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent ev) { } public void mouseMoved(MouseEvent ev) { hotX = ev.getX(); hotY = ev.getY(); setActive(ev.getX(), ev.getY()); if(mouseActive != null && (mouseActive instanceof BX)) { BX bx = (BX)mouseActive; Activator.desktop.setSelected(bx.b); } } }); addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent ev) { requestFocus(); hotX = ev.getX(); hotY = ev.getY(); setActive(ev.getX(), ev.getY()); if(mouseActive != null && (mouseActive instanceof BX)) { BX bx = (BX)mouseActive; Activator.desktop.setSelected(bx.b); } } }); center = new SpinItem() { public void paint(Graphics g) { }; public void paintDependencies(Graphics g) { }; public void paintInfo(Graphics g, double x, double y) { }; public boolean isActive() { return false; } }; addKeyListener(kl = new KeyAdapter() { public void keyPressed(KeyEvent e) { if(bShowConsole) { handleConsole(e); repaint(); return; } if(bSearchMode) { switch(e.getKeyCode()) { case KeyEvent.VK_PERIOD: bSearchMode = false; break; case KeyEvent.VK_DELETE: case KeyEvent.VK_BACK_SPACE: if(searchString.length() > 0) { searchString = searchString.substring(0, searchString.length() -1); doSearch(); } break; case KeyEvent.VK_UNDERSCORE: searchString = searchString + "_"; doSearch(); default: searchString = (searchString + e.getKeyChar()).toLowerCase(); doSearch(); } repaint(); return; } switch(e.getKeyCode()) { case KeyEvent.VK_SLASH: case KeyEvent.VK_PERIOD: bSearchMode = true; break; case KeyEvent.VK_F: bShowFrameRate = !bShowFrameRate; break; case KeyEvent.VK_B: bShowBundleLegend = !bShowBundleLegend; break; case KeyEvent.VK_I: bShowBundleInfo = !bShowBundleInfo; break; case KeyEvent.VK_0: { if(mouseActive != null) { final SpinItem item = mouseActive; Runnable run = new Runnable() { public void run() { while(deltaA < 0) deltaA += Math.PI * 2; while(deltaA > -item.getAngle()) { deltaA -= Math.PI / 50; bxNeedRecalc = true; sxNeedRecalc = true; repaint(); try { Thread.sleep(20); } catch (Exception e) { } } deltaA = -item.getAngle(); } }; Thread t = new Thread(run, "spin thread"); t.start(); } } break; case KeyEvent.VK_8: deltaA -= aStep; bxNeedRecalc = true; sxNeedRecalc = true; break; case KeyEvent.VK_9: deltaA += aStep; bxNeedRecalc = true; sxNeedRecalc = true; break; case KeyEvent.VK_LEFT: step(-1); break; case KeyEvent.VK_RIGHT: step(1); break; case KeyEvent.VK_F1: case KeyEvent.VK_H: case KeyEvent.VK_HELP: bShowHelp = !bShowHelp; break; case KeyEvent.VK_D: bShowDeps = !bShowDeps; break; case KeyEvent.VK_2: use2D = !use2D; break; case KeyEvent.VK_S: bStepSize = !bStepSize; initFonts(); break; case KeyEvent.VK_4: fontSize += .1; initFonts(); break; case KeyEvent.VK_3: fontSize = Math.max(.2, fontSize - .1); initFonts(); break; case KeyEvent.VK_C: active.clear(); depVector = null; depActive = null; break; case KeyEvent.VK_TAB: stepDependency(); break; case KeyEvent.VK_F2: toggleConsole(); break; case KeyEvent.VK_SPACE: if(depActive != null) { mouseActive = depActive; depActive = null; depVector = null; active.clear(); } if(mouseActive != null) { if(active.containsKey(mouseActive)) { active.remove(mouseActive); } else { active.put(mouseActive, mouseActive); } } break; } repaint(); } }); } void handleConsole(KeyEvent e) { } boolean bShowConsole = false; int lineCount = 0; void toggleConsole() { if(console == null) { console = new Console(this); } bShowConsole = !bShowConsole; lineCount++; console.addLine("count=" + lineCount); if(bShowConsole) { console.setBounds(10, size.height / 2, size.width - 10, size.height - 10); } else { } repaint(); } Vector depVector = null; int depPos = 0; SpinItem depActive = null; void stepDependency() { if(depVector == null && mouseActive != null) { depVector = mouseActive.getNext(SpinItem.DIR_FROM | SpinItem.DIR_TO); depPos = 0; } if(depVector != null && depVector.size() > 0) { depPos = (depPos + 1) % depVector.size(); depActive = (SpinItem)depVector.elementAt(depPos); repaint(); } } void doSearch() { active.clear(); int hitCount = 0; SpinItem hit = null; if(searchString.length() > 0) { for(Iterator it = bundles.keySet().iterator(); it.hasNext(); ) { Long key = (Long)it.next(); SpinItem item = (SpinItem)bundles.get(key); if(item.toString().toLowerCase().startsWith(searchString)) { active.put(item, item); hit = item; hitCount++; } } } mouseActive = hit; depActive = null; } void step(int delta) { if(mouseActive != null && (mouseActive instanceof BX)) { BX bx = (BX)mouseActive; long id = bx.b.getBundleId(); BX bx2 = getBX(id + delta); if(bx2 != null) { mouseActive = bx2; repaint(); } } if(mouseActive != null && (mouseActive instanceof SX)) { SX sx = (SX)mouseActive; Long id = (Long)sx.sr.getProperty("service.id"); SX sx2 = getSX(id.longValue() + delta); if(sx2 != null) { mouseActive = sx2; repaint(); } } } int fontMin = 1; int fontMax = 11; Font[] fonts; void initFonts() { initFonts("Dialog"); } void initFonts(String name) { fonts = new Font[fontMax - fontMin + 1]; int n = 0; for(int i = fontMin; i <= fontMax; i++) { int h = (int)(fontSize * i); if(bStepSize && i < fontMax) { h = (int)(fontMin * fontSize); } Font f = new Font("Dialog", Font.PLAIN, h); fonts[n++] = f; } } SpinItem mouseActive = null; void setActive(int x, int y) { double minD = 1000000; SpinItem nearest = null; for(Iterator it = services.keySet().iterator(); it.hasNext(); ) { Object key = it.next(); SpinItem item = (SpinItem)services.get(key); double dx = x - item.getSX(); double dy = y - item.getSY(); double d2 = dx * dx + dy * dy; if(d2 < minD) { minD = d2; nearest = item; } } for(Iterator it = bundles.keySet().iterator(); it.hasNext(); ) { Long key = (Long)it.next(); SpinItem item = (SpinItem)bundles.get(key); double dx = x - item.getSX(); double dy = y - item.getSY(); double d2 = dx * dx + dy * dy; if(d2 < minD) { minD = d2; nearest = item; } } if(nearest != null) { mouseActive = nearest; } repaint(); } Font getFont(double k) { int i = (int)(fontMin + (fontMax - fontMin) * k); i = Math.max(0, Math.min(fonts.length - 1, i)); return fonts[i]; } public void forceRepaint() { bxNeedRecalc = true; sxNeedRecalc = true; // System.out.println("forceRepaint"); repaint(); } boolean bxNeedRecalc = false; boolean sxNeedRecalc = false; public void bundleChanged(BundleEvent ev) { Long id = new Long(ev.getBundle().getBundleId()); synchronized(bundles) { BX bx = (BX)bundles.get(id); switch(ev.getType()) { case BundleEvent.INSTALLED: if(bx == null) { bx = new BX(this, ev.getBundle()); bundles.put(id, bx); bxNeedRecalc = true; } break; case BundleEvent.UNINSTALLED: if(bx != null) { bundles.remove(id); bxNeedRecalc = true; } break; } } repaint(); } PackageAdmin pkgAdmin = null; public void serviceChanged(ServiceEvent ev) { synchronized(services) { ServiceReference sr = ev.getServiceReference();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -