📄 obrdisplayer.java
字号:
/* * Copyright (c) 2004, 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.osgi.bundle.bundlerepository.desktop;import org.osgi.framework.*;import org.osgi.util.tracker.*;import java.util.*;import org.knopflerfish.service.desktop.*;import org.ungoverned.osgi.service.bundlerepository.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;import javax.swing.tree.*;import java.net.*;import java.io.*;import java.lang.reflect.Array;/** * Desktop plugin for the BundleRepositoryService * with functionality for install/start and detail information. */public class OBRDisplayer extends DefaultSwingBundleDisplayer { static ServiceTracker obrTracker; // Shared by all instances of JOBRAdmin static ImageIcon startIcon; static ImageIcon sortIcon; static ImageIcon installIcon; static ImageIcon updateIcon; static ImageIcon bundleIcon; static ImageIcon reloadIcon; static final int SORT_NONE = 0; static final int SORT_HOST = 1; static final int SORT_CATEGORY = 2; static final int SORT_VENDOR = 3; static final int SORT_APIVENDOR = 4; static final int SORT_STATUS = 5; static int[] SORT_ARRAY = new int[] { SORT_NONE, SORT_HOST, SORT_CATEGORY, SORT_VENDOR, SORT_STATUS, }; static String[] SORT_NAMES = new String[] { "All", "Host", "Category", "Vendor", "Install status", }; // Error message for all instances. // since the OBR service is shared, the err message // can be shared too String obrErr = ""; // Message while loading repo URLs static String STR_LOADING = "Loading..."; // Name of top node static String STR_TOPNAME = "Bundle Repository"; public OBRDisplayer(BundleContext bc) { super(bc, "Bundle Repository", "View and install bundles from Bundle Repository", true); try { // share icon instances between instances if(startIcon == null) { startIcon = new ImageIcon(getClass().getResource("/player_play.png")); installIcon = new ImageIcon(getClass().getResource("/player_install.png")); updateIcon = new ImageIcon(getClass().getResource("/update.png")); sortIcon = new ImageIcon(getClass().getResource("/sort_select.png")); bundleIcon = new ImageIcon(getClass().getResource("/lib16x16.png")); reloadIcon = new ImageIcon(getClass().getResource("/reload_green.png")); } } catch (Exception e) { System.err.println("icon load failed: " + e); } obrTracker = new ServiceTracker(bc, BundleRepositoryService.class.getName(), null); obrTracker.open(); } BundleRepositoryService getOBR() { return (BundleRepositoryService)OBRDisplayer.obrTracker.getService(); } public JComponent newJComponent() { return new JOBRAdmin(); } public void disposeJComponent(JComponent comp) { JOBRAdmin obrAdmin = (JOBRAdmin)comp; obrAdmin.stop(); super.disposeJComponent(comp); } void closeComponent(JComponent comp) { JOBRAdmin obrAdmin = (JOBRAdmin)comp; obrAdmin.stop(); } public void valueChanged(final long bid) { super.valueChanged(bid); for(Iterator it = components.iterator(); it.hasNext(); ) { JOBRAdmin obrAdmin = (JOBRAdmin)(JComponent)it.next(); obrAdmin.valueChanged(bid); } } public Icon getSmallIcon() { return null; } /** * The actual component returned by newJComponent */ class JOBRAdmin extends JPanel { DefaultTreeModel treeModel; JTree recordTree; JPanel recordPanel; JButton installButton; JButton refreshButton; JButton startButton; JButton updateButton; JTextPane html; JScrollPane htmlScroll; TopNode rootNode; // currently selected node OBRNode brSelected = null; JMenuItem contextItem; JPopupMenu contextPopupMenu; // Category used for grouping bubdle records int sortCategory = SORT_CATEGORY; // Map of all bundles in OBR tree // String (location) -> OBRNode Map locationMap = new HashMap(); public JOBRAdmin() { setLayout(new BorderLayout()); recordTree = new JTree(new TopNode("[not loaded]")); recordTree.setRootVisible(true); // Must be registered for renderer tooltips to work ToolTipManager.sharedInstance().registerComponent(recordTree); // Load leaf icon for the tree cell renderer. TreeCellRenderer renderer = new DefaultTreeCellRenderer() { public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) { Component c = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus); TreePath tp = tree.getPathForRow(row); try { Object node = tp.getLastPathComponent(); String tt = null; if(node instanceof OBRNode) { OBRNode obrNode = (OBRNode)node; setIcon(obrNode.bBusy ? reloadIcon : bundleIcon); String loc = (String)obrNode.getBundleRecord().getAttribute(BundleRecord.BUNDLE_UPDATELOCATION); tt = obrNode.bBusy ? "busy..." : loc; boolean bInstalled = isInstalled(obrNode.getBundleRecord()); obrNode.setInstalled(bInstalled); if(bInstalled) { setForeground(Color.gray); } } else if(node instanceof TopNode) { TopNode topNode = (TopNode)node; if(STR_LOADING.equals(topNode.name)) { setIcon(reloadIcon); } } else { // setIcon(null); } setToolTipText(tt); } catch (Exception ignored ) { } return this; } }; recordTree.setCellRenderer(renderer); // call setSelected() when user selects nodes/leafs in the tree recordTree.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent e) { TreePath[] sel = recordTree.getSelectionPaths(); if(sel != null && sel.length == 1) { setSelected((TreeNode)sel[0].getLastPathComponent()); } else { setSelected(null); } } }); // Create the HTML text pane for detail view. // The node's getTitle()/toHTML() methods // will be called whenever a node is HTMLAble html = new JTextPane(); html.setText(""); html.setContentType("text/html"); html.setEditable(false); html.addHyperlinkListener(new HyperlinkListener() { public void hyperlinkUpdate(HyperlinkEvent ev) { if (ev.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { URL url = ev.getURL(); try { Util.openExternalURL(url); } catch (Exception e) { // Activator.log.warn("Failed to open external url=" + url, e); } } } }); htmlScroll = new JScrollPane(html, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); htmlScroll.setPreferredSize(new Dimension(300, 300)); JScrollPane treeScroll = new JScrollPane(recordTree, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); treeScroll.setPreferredSize(new Dimension(200, 300)); JButton repoButton = new JButton("URLs"); repoButton.setToolTipText("Show/set repository URLs"); repoButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { askRepoURls(); } }); installButton = new JButton(installIcon); installButton.setToolTipText("Install from OBR"); ActionListener installAction; installButton.addActionListener(installAction = new ActionListener() { public void actionPerformed(ActionEvent ev) { installOrStart(brSelected, false); } }); installButton.setEnabled(false); startButton = new JButton(startIcon); startButton.setToolTipText("Install and start from OBR"); ActionListener startAction; startButton.addActionListener(startAction = new ActionListener() { public void actionPerformed(ActionEvent ev) { installOrStart(brSelected, true); } }); startButton.setEnabled(false); refreshButton = new JButton(reloadIcon); refreshButton.setToolTipText("Refresh OBR list"); refreshButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { refreshList(true); } }); recordPanel = new JPanel(new BorderLayout()); recordPanel.add(htmlScroll, BorderLayout.CENTER); JPanel left = new JPanel(new BorderLayout()); left.add(treeScroll, BorderLayout.CENTER); JToolBar leftTools = new JToolBar(); leftTools.add(makeSortSelectionButton()); leftTools.add(refreshButton); leftTools.add(installButton); leftTools.add(startButton); leftTools.add(repoButton); left.add(leftTools, BorderLayout.SOUTH); // create a context menu which copies the names // and actions from the start and stop buttons. contextPopupMenu = new JPopupMenu(); contextItem = new JMenuItem("------------"); contextItem.setEnabled(false); contextPopupMenu.add(contextItem); contextPopupMenu.add(new JPopupMenu.Separator()); JMenuItem item; item = new JMenuItem(startButton.getToolTipText(), startButton.getIcon()); item.addActionListener(startAction); contextPopupMenu.add(item); item = new JMenuItem(installButton.getToolTipText(), installButton.getIcon()); item.addActionListener(installAction); contextPopupMenu.add(item); // add listener for tree context menu, which selects the // item belo the mouse and pops up the context menu. recordTree.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { maybeShowPopup(e); } public void mouseReleased(MouseEvent e) { maybeShowPopup(e); } private void maybeShowPopup(MouseEvent e) { int mod = e.getModifiers(); if(contextPopupMenu != null && (e.isPopupTrigger() || ((e.getModifiers() & InputEvent.BUTTON2_MASK) != 0))) { TreePath tp = recordTree.getPathForLocation(e.getX(), e.getY()); if(tp != null) { TreeNode node = (TreeNode)tp.getLastPathComponent(); if(node instanceof OBRNode) { contextItem.setText(((OBRNode)node).name); recordTree.setSelectionPath(tp); setSelected(node); Component comp = e.getComponent(); contextPopupMenu.show(comp, e.getX(), e.getY()); } } } } }); JSplitPane panel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, left, recordPanel); panel.setDividerLocation(200); add(panel, BorderLayout.CENTER); refreshList(true); } /** * Called when BundleSelectionListener distributes events. * * <p> * If possible, select and show the bundle with id <tt>bid</tt> * in the tree. * </p> */ void valueChanged(long bid) { try { if(bid >= 0) { Bundle b = bc.getBundle(bid); if(b != null) { OBRNode obrNode = getOBRNode(b); if(obrNode != null && obrNode != brSelected) { TreePath tp = new TreePath(obrNode.getPath()); showPath(tp, null); } } } } catch (Exception e) { } } /** * Get the OBRNode tree node which matches a bundle. * * @return OBRNode for the specified bundle if it exists, * <tt>null</tt> otherwise. */ OBRNode getOBRNode(Bundle b) { OBRNode node = (OBRNode)locationMap.get(b.getLocation()); if(node != null) { return node;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -