2012-11-18 18 views
0

我在使用infonode對接庫時遇到了問題。我已經根據所提供的示例創建了根窗口,動態視圖方法。只是修改它,只是試圖使用它。但我已經通過infonode提供的文檔,用戶手冊。但coludnt找到了一種將動態視圖添加到根窗口的方法。如何使用Infonode庫將生成的動態視圖添加到根窗口?

這裏是一個sampple代碼:

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Component; 
import java.awt.Dimension; 
import java.awt.FlowLayout; 
import java.awt.Graphics; 
import java.awt.Point; 
import java.awt.Window; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.MouseEvent; 
import java.io.IOException; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.util.HashMap; 

import javax.sound.sampled.ReverbType; 
import javax.swing.*; 

import net.infonode.docking.*; 
import net.infonode.docking.drag.DockingWindowDragSource; 
import net.infonode.docking.drag.DockingWindowDragger; 
import net.infonode.docking.drag.DockingWindowDraggerProvider; 
import net.infonode.docking.util.DockingUtil; 
import net.infonode.docking.util.MixedViewHandler; 
import net.infonode.docking.util.ViewMap; 




public class DockTrial extends JFrame 
{ 

    JMenuBar menuBar; 
    JMenu fileMenu; 
    JMenuItem fMI; 
    int counter=0; 
    CustomRootWindow cRootWin; 

    private static final int ICON_SIZE = 8; 
    /** 
     * Contains the dynamic views that has been added to the root window 
     */ 
     private HashMap dynamicViews = new HashMap(); 

     /** 
     * The one and only root window 
     */ 
     private RootWindow rootWindow; 

     /** 
     * Contains all the static views 
     */ 
     private ViewMap viewMap = new ViewMap(); 

     /** 
     * An array of the static views 
     */ 
     private View[] views = new View[3]; 

     /** 
     * A dynamically created view containing an id. 
     */ 
     private static class DynamicView extends View { 
     private int id; 

     /** 
     * Constructor. 
     * 
     * @param title  the view title 
     * @param icon  the view icon 
     * @param component the view component 
     * @param id  the view id 
     */ 
     DynamicView(String title, Icon icon, Component component, int id) { 
      super(title, icon, component); 
      this.id = id; 
     } 

     /** 
     * Returns the view id. 
     * 
     * @return the view id 
     */ 
     public int getId() { 
      return id; 
     } 
     } 


    public DockTrial() 
    { 
     setSize(500,500); 
     setTitle("Docktrial"); 
     setVisible(true); 
     setLayout(new BorderLayout()); 
     add(menuBarr()); 
     setJMenuBar(menuBar); 
     createComponents(); 
     assignfunctions(); 
     add(createToolBar(),BorderLayout.BEFORE_FIRST_LINE); 
     getContentPane().add(rootWindow, BorderLayout.CENTER); 

    } 

    public void createComponents() 
    { 
     // Create the views 
     for (int i = 0; i < views.length; i++) { 
      views[i] = new View("View " + i, VIEW_ICON, createViewComponent("View " + i)); 
      viewMap.addView(i, views[i]); 
     } 

     // The mixed view map makes it easy to mix static and dynamic views inside the same root window 
     MixedViewHandler handler = new MixedViewHandler(viewMap, new ViewSerializer() { 
      public void writeView(View view, ObjectOutputStream out) throws IOException { 
     out.writeInt(((DynamicView) view).getId()); 
      } 

      public View readView(ObjectInputStream in) throws IOException { 
     return getDynamicView(in.readInt()); 
      } 
     }); 

     rootWindow = DockingUtil.createRootWindow(viewMap, handler, true); 
    } 

    public void assignfunctions() 
    { 
    fMI.addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent arg0) { 
     /* FloatingWindow fw = rootWindow.createFloatingWindow(new Point(50, 50), 
       new Dimension(300, 200), 
       getDynamicView(getDynamicViewId())); 

       // Show the window 
       fw.getTopLevelAncestor().setVisible(true); 
       fw.isDockable();*/ 

       /*getDynamicView(getDynamicViewId()).dock(); 
       rootWindow.revalidate();*/ 
     } 
    }); 
    } 

    /** 
     * Creates the frame tool bar. 
     * 
     * @return the frame tool bar 
     */ 
     private JToolBar createToolBar() { 
     JToolBar toolBar = new JToolBar(); 
     JLabel label = new JLabel("Drag New View"); 
     toolBar.add(label); 
     new DockingWindowDragSource(label, new DockingWindowDraggerProvider() { 
      public DockingWindowDragger getDragger(MouseEvent mouseEvent) { 

     return getDynamicView(getDynamicViewId()).startDrag(rootWindow); 
      } 
     }); 
     return toolBar; 
     } 

    /** 
     * Returns a dynamic view with specified id, reusing an existing view if possible. 
     * 
     * @param id the dynamic view id 
     * @return the dynamic view 
     */ 
     private View getDynamicView(int id) { 
     View view = (View) dynamicViews.get(new Integer(id)); 

     if (view == null) 
      view = new DynamicView("Dynamic View " + id, VIEW_ICON, createViewComponent("Dynamic View " + id), id); 

     return view; 
     } 

    /** 
     * Returns the next available dynamic view id. 
     * 
     * @return the next available dynamic view id 
     */ 
     private int getDynamicViewId() { 
     int id = 0; 

     while (dynamicViews.containsKey(new Integer(id))) 
      id++; 

     return id; 
     } 

    /** 
     * Returns a dynamic view with specified id, reusing an existing view if possible. 
     * 
     * @param id the dynamic view id 
     * @return the dynamic view 
     */ 
     private View getDynamicView(int id ,String t) { 
     View view = (View) dynamicViews.get(new Integer(id)); 

     if (view == null) 
      view = new DynamicView("Dynamic View " + id + t, VIEW_ICON, createViewComponent("Dynamic View " + id), id); 

     return view; 
     } 


     /** 
     * Creates a view component containing the specified text. 
     * 
     * @param text the text 
     * @return the view component 
     */ 
     private static JComponent createViewComponent(String text) { 
     StringBuffer sb = new StringBuffer(); 

     for (int j = 0; j < 100; j++) 
      sb.append(text + ". This is line " + j + "\n"); 

     return new JScrollPane(new JTextArea(sb.toString())); 
     } 

     /** 
     * Custom view icon. 
     */ 
     private static final Icon VIEW_ICON = new Icon() { 
     public int getIconHeight() { 
      return ICON_SIZE; 
     } 

     public int getIconWidth() { 
      return ICON_SIZE; 
     } 

     public void paintIcon(Component c, Graphics g, int x, int y) { 
      Color oldColor = g.getColor(); 

      g.setColor(new Color(70, 70, 70)); 
      g.fillRect(x, y, ICON_SIZE, ICON_SIZE); 

      g.setColor(new Color(100, 230, 100)); 
      g.fillRect(x + 1, y + 1, ICON_SIZE - 2, ICON_SIZE - 2); 

      g.setColor(oldColor); 
     } 
     }; 

    public JMenuBar menuBarr() 
    { 
     menuBar = new JMenuBar(); 
     fileMenu = new JMenu("File"); 
     fMI = new JMenuItem("New Tab"); 
     fileMenu.add(fMI); 
     menuBar.add(fileMenu); 
     return menuBar; 
    } 

    public static void main(String[] args) 
    { 
     new DockTrial(); 
    } 


} 

回答

1

我評價這個框架了。答案是使用DockingUtil.addwindow。這是示例。

public TestFrame() { 
     super("Test Dock"); 
     rw = new RootWindow(null); 
     JMenu mu = new JMenu("File"); 
     JMenuItem mi = new JMenuItem("Add Dock"); 
     mi.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       View vw = new View("Dock", null, new JLabel("test")); 
       DockingUtil.addWindow(vw, rw); 
      } 
     }); 
     JMenuBar mb = new JMenuBar(); 
     mu.add(mi); 
     mb.add(mu); 
     this.setJMenuBar(mb); 
     this.setLayout(new BorderLayout()); 
     this.add(rw); 

    } 
相關問題