📄 spin.java
字号:
Object obj = Activator.getTargetBC().getService(sr); boolean bRelease = true; SX sx = (SX)services.get(sr); switch(ev.getType()) { case ServiceEvent.REGISTERED: if(obj instanceof PackageAdmin) { if(pkgAdmin == null) { pkgAdmin = (PackageAdmin)obj; bRelease = false; } } if(sx == null) { sx = new SX(this,sr); services.put(sr, sx); sxNeedRecalc = true; } break; case ServiceEvent.UNREGISTERING: if(obj instanceof PackageAdmin) { if(pkgAdmin != null) { pkgAdmin = null; bRelease = true; } } if(sx != null) { services.remove(sr); sxNeedRecalc = true; } break; case ServiceEvent.MODIFIED: break; } if(bRelease) { Activator.getTargetBC().ungetService(sr); } } repaint(); } void recalcBundlePositions() { if(memG == null) { System.out.println("recalc - no memG"); return; } synchronized(bundles) { int i = 0; int n = bundles.size(); int cx = size.width / 2; int cy = size.height / 2; for(Iterator it = bundles.keySet().iterator(); it.hasNext(); ) { Long id = (Long)it.next(); BX bx = (BX)bundles.get(id); double a = deltaA + i * Math.PI * 2 / n; bx.setPos(cx - 20 + cx * .8 * Math.cos(a), cy + cy * .8 * Math.sin(a)); bx.setAngle(a); i++; } } } void recalcServicePositions() { if(memG == null) { System.out.println("sx recalc - no memG"); return; } synchronized(services) { int cx = size.width / 2; int cy = size.height / 2; for(Iterator it = services.keySet().iterator(); it.hasNext(); ) { ServiceReference sr = (ServiceReference)it.next(); SX sx = (SX)services.get(sr); BX bx = (BX)bundles.get(sx.bid); if(bx == null) { System.out.println("No bundle for " + sx); continue; } double x = Math.random() * 10 + bx.x - (bx.x - cx) * .2; double y = Math.random() * 10 + bx.y - (bx.y - cy) * .2; sx.setPos(x, y); } } } int count = 0; public void run() { try { repaint(); while(memG == null) { Thread.sleep(100); } while(bRun) { long startTime = System.currentTimeMillis(); synchronized(paintLock) { updateAll(); paintAll(); } updateTime = System.currentTimeMillis() - startTime; long delta = targetTime - updateTime; delay = Math.max(2, delta); Thread.sleep(delay); totalTime = System.currentTimeMillis() - startTime; } } catch (Exception e) { e.printStackTrace(); } } public void updateAll() { count++; } public void paintAll() { synchronized(paintLock) { Graphics g = getGraphics(); if(g != null) { paint(g); g.dispose(); } } } String indent(int level) { StringBuffer sb = new StringBuffer(); while(level --> 0) sb.append(" "); return sb.toString(); } void getDeps(StringBuffer sb, Hashtable lines, SpinItem item, int level) { if(item == null) return; String line = item.toString(); if(lines.containsKey(line)) { return; } String s = indent(level); // System.out.println(level + ": " + s + item.toString()); if(!line.equals("System Bundle")) { sb.append(s + item.toString() + "\n"); lines.put(line, line); } Vector v = item.getNext(SpinItem.DIR_FROM); if(v != null) { if(level > 3 && v.size() > 0) { sb.append(indent(level) + "...\n"); return; } for(int i = 0; i < v.size(); i++) { SpinItem next = (SpinItem)v.elementAt(i); getDeps(sb, lines, next, level + 1); } } } void paintDeps(Graphics g, int x, int y) { SpinItem item = getInfoItem(); if(item == null) return; StringBuffer sb = new StringBuffer(); getDeps(sb, new Hashtable(), item, 0); String name = ""; if(item instanceof SX) { name = "(" + BX.shortName(((SX)item).sr.getBundle()) + ")\n"; } String s = "Imports from:\n" + name + sb.toString(); paintBox(s, g, Color.white, Color.black, x, y, .8, 200, 200); } void paintHelp(Graphics g, int x, int y) { paintBox("F1/H \t- toggle help\n" + "B \t- toggle legend\n" + "I \t- toggle detail info\n" + "D \t- toggle dependencies\n" + "left \t- previous item\n" + "right\t- next item\n" + "space\t- mark item\n" + "C \t- clear all marked\n" + "Tab \t- step dependencies\n" + "2 \t- toggle Graphics2D\n" + "S \t- toggle step font size\n" + "3 \t- decrease font size\n" + "4 \t- increase font size\n" + "8 \t- rotate anti-clockwise\n" + "9 \t- rotate clockwise\n" + ". \t- start/stop search mode\n", g, Color.white, Color.black, x, y); } void paintBox(String s, Graphics g, Color bgCol, Color fgCol, int x, int y) { paintBox(s, g, bgCol, fgCol, x, y, 1.0, 200, 200); } void paintBox(String s, Graphics g, Color bgCol, Color fgCol, int x, int y, double size, int minWidth, int minHeight) { Object oldComp = null; if(use2D) { Graphics2D g2 = (Graphics2D)g; oldComp = g2.getComposite(); g2.setComposite((AlphaComposite)alphaHalf); } String[] lines = split(s, "\n"); int maxCols = 0; for(int i = 0; i <lines.length; i++) { if(lines[i].length() > maxCols) { maxCols = lines[i].length(); } } Font font = getFont(size); g.setColor(bgCol); g.fill3DRect(x, y, Math.max(minWidth, font.getSize() * maxCols / 2 + 30), Math.max(minHeight, (font.getSize() + 3) * lines.length + 10), true); g.setFont(font); g.setColor(fgCol); x += 10; y += font.getSize() + 5; int x2 = x + font.getSize() * 8; if(use2D) { if(oldComp != null) { Graphics2D g2 = (Graphics2D)g; g2.setComposite((AlphaComposite)oldComp); } } for(int i = 0; i <lines.length; i++) { int ix = lines[i].indexOf("\t"); if(ix != -1) { g.drawString(lines[i].substring(0, ix), x, y); g.drawString(lines[i].substring(ix + 1), x+font.getSize()*4, y); } else { g.drawString(lines[i], x, y); } y += font.getSize() + 3; } } void paintBundles(Graphics g) { // System.out.println("paintBundles()"); if(bxNeedRecalc) { recalcBundlePositions(); bxNeedRecalc = false; } synchronized(bundles) { for(Iterator it = bundles.keySet().iterator(); it.hasNext(); ) { Long id = (Long)it.next(); BX bx = (BX)bundles.get(id); bx.paint(g); } } } /** * Splits a string into words, using the <code>StringTokenizer</code> class. */ public static String[] split(String s, String sep) { StringTokenizer st = new StringTokenizer(s, sep); int ntok = st.countTokens(); String[] res = new String[ntok]; for (int i = 0; i < ntok; i++) { res[i] = st.nextToken(); } return res; } void zoom() { toScreen(center); for(Iterator it = bundles.keySet().iterator(); it.hasNext(); ) { Long id = (Long)it.next(); BX bx = (BX)bundles.get(id); toScreen(bx); } for(Iterator it = services.keySet().iterator(); it.hasNext(); ) { ServiceReference sr = (ServiceReference)it.next(); SX sx = (SX)services.get(sr); toScreen(sx); } } void paintServices(Graphics g) { // System.out.println("paintServices()"); if(sxNeedRecalc) { recalcServicePositions(); sxNeedRecalc = false; } synchronized(services) { for(Iterator it = services.keySet().iterator(); it.hasNext(); ) { ServiceReference sr = (ServiceReference)it.next(); SX sx = (SX)services.get(sr); sx.paint(g); } } } boolean isActive(SpinItem item) { return active.containsKey(item) || item == mouseActive || item == depActive; } boolean isPainting = false; boolean bAntiAlias = true; public void setAntiAlias(boolean b) { bAntiAlias = b; } public boolean getAntiAlias() { return bAntiAlias; } long bgTime, spriteTime, fgTime; public void update(Graphics g) { paint(g); } public void paint(Graphics _g) { // System.out.println("paint()"); if(isPainting) return; isPainting = true; synchronized(paintLock) { zoom(); if(use2D) { if(alphaHalf == null) { alphaHalf = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .7f); } } Graphics g = _g; makeOff(false); if(memG == null) return; long start = System.currentTimeMillis(); paintBg(memG); bgTime = System.currentTimeMillis() - start; if(use2D) { ((Graphics2D)memG). setRenderingHint(RenderingHints.KEY_ANTIALIASING, bAntiAlias ? RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF); } paintBundles(memG); spriteTime = System.currentTimeMillis() - bgTime - start; paintServices(memG); fgTime = System.currentTimeMillis() - spriteTime - start; if(bShowBundleLegend) { paintBundleStateLegend(memG); } SpinItem infoItem = getInfoItem(); if(infoItem != null) { if(bShowBundleInfo) { infoItem.paintInfo(memG, 10, size.height - 100); } } if(bShowConsole) { console.paint(memG); } if(bShowFrameRate) { paintFrameRate(memG); } if(bShowHelp) { paintHelp(memG, size.width/2-100, 50); } if(bShowDeps) { paintDeps(memG, (hotX > size.width / 2) ? 40 : (size.width / 2), 20); } if(bSearchMode) { paintSearch(memG, 10, size.height - 140); } if(use2D) { ((Graphics2D)memG). setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); } g.drawImage(memImage, 0, 0, null); } isPainting = false; } public SpinItem getInfoItem() { return depActive != null ? depActive : mouseActive; } public void paintBundleStateLegend(Graphics g) { paintCBox(g, 10, 10, BX.getColor(Bundle.INSTALLED), "Installed"); paintCBox(g, 10, 25, BX.getColor(Bundle.ACTIVE), "Active"); paintCBox(g, 10, 40, BX.getColor(Bundle.RESOLVED), "Resolved"); paintCBox(g, 10, 55, BX.getColor(Bundle.UNINSTALLED), "Uninstalled"); paintCBox(g, 10, 70, BX.getColor(Bundle.STOPPING), "Stopping"); paintCBox(g, 10, 85, BX.getColor(Bundle.STARTING), "Starting"); paintCBox(g, 10, 105, BX.importsFromColor, "Imports from"); paintCBox(g, 10, 120, BX.exportsToColor, "Exports to"); paintCBox(g, 10, size.height - 20, bgColor, "F1 - help"); } public void paintCBox(Graphics g, int x, int y, Color c, String text) { int w = 12; int h = 12; g.setColor(c); g.fillRect(x, y, w, h); g.setColor(Color.gray); g.drawLine(x, y, x+w, y); g.drawLine(x, y, x, y+h); g.setColor(Color.black); g.drawLine(x+w, y, x+w, y+h); g.drawLine(x, y+h, x+w, y+h); g.setColor(textColor); g.setFont(getFont(1.0)); g.drawString(text, x + 30, y + 10); } public void paintFrameRate(Graphics g) { g.setColor(Color.black); g.drawString("fps: " + (int)(1000.0 / totalTime), 10, size.height - 5); g.drawString("delay: " + delay, 90, size.height - 5);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -