1 package vtk.sample;
2 
3 import java.awt.BorderLayout;
4 import java.awt.Dimension;
5 import java.awt.Toolkit;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.WindowAdapter;
8 import java.awt.event.WindowEvent;
9 import java.awt.event.WindowListener;
10 
11 import javax.swing.AbstractAction;
12 import javax.swing.JDesktopPane;
13 import javax.swing.JFrame;
14 import javax.swing.JInternalFrame;
15 import javax.swing.JMenu;
16 import javax.swing.JMenuBar;
17 import javax.swing.JPopupMenu;
18 import javax.swing.JSplitPane;
19 import javax.swing.JTabbedPane;
20 import javax.swing.SwingUtilities;
21 
22 import vtk.vtkNativeLibrary;
23 
24 public class InternalFrames extends JFrame {
25   private static final long serialVersionUID = 1L;
26   private Desktop theDesktop;
27   private Dimension screenSize;
28 
29   // -----------------------------------------------------------------
30   // Load VTK library and print which library was not properly loaded
31   static {
32     if (!vtkNativeLibrary.LoadAllNativeLibraries()) {
33       for (vtkNativeLibrary lib : vtkNativeLibrary.values()) {
34         if (!lib.IsLoaded()) {
lib.GetLibraryName()35           System.out.println(lib.GetLibraryName() + " not loaded");
36         }
37       }
38     }
39     vtkNativeLibrary.DisableOutputWindow(null);
40   }
41 
42   // -----------------------------------------------------------------
43 
InternalFrames()44   public InternalFrames() {
45     super("VTK Internal Frame Demo");
46     screenSize = Toolkit.getDefaultToolkit().getScreenSize();
47     this.setSize(900, 900);
48 
49     WindowListener l = new WindowAdapter() {
50       public void windowClosing(WindowEvent e) {
51         System.exit(0);
52       }
53     };
54     this.addWindowListener(l);
55 
56     this.getContentPane().add(new SplitFrame());
57 
58     new MenuMgr();
59 
60     this.setVisible(true);
61   }
62 
addMenuBar(JMenuBar m)63   public void addMenuBar(JMenuBar m) {
64     setJMenuBar(m);
65   }
66 
67   private class SplitFrame extends JSplitPane {
68     private static final long serialVersionUID = 1L;
69 
SplitFrame()70     public SplitFrame() {
71       super(JSplitPane.VERTICAL_SPLIT);
72       this.setDividerLocation(screenSize.height / 2);
73       setContinuousLayout(true);
74       setOneTouchExpandable(true);
75       add(theDesktop = new Desktop());
76       add(new Tabbed());
77     }
78   }
79 
80   private class Desktop extends JDesktopPane {
81     private static final long serialVersionUID = 1L;
82 
Desktop()83     public Desktop() {
84       super();
85       this.setPreferredSize(screenSize);
86       this.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
87       this.add(new VTKFrame(10, 10));
88       this.add(new VTKFrame(500, 10));
89     }
90   }
91 
92   private class Tabbed extends JTabbedPane {
93     private static final long serialVersionUID = 1L;
94 
Tabbed()95     public Tabbed() {
96       this.addTab("vtk1", new VTKCanvas());
97       this.addTab("vtk2", new VTKCanvas());
98 
99       setMinimumSize(new Dimension(300, 300));
100       // another swing bug work around
101       // for some reason the first time the tabbed item is drawn,
102       // tab 0 is selected but the sphere (tab 1) is drawn
103       // manually selecting the tabs synchs the tab and the draw order
104       // it is harmless but it is still annoying
105       // so we force tab 1 to be selected and hide the bug from the user
106       // note that selecting 0 does not work....
107       this.setSelectedIndex(1);
108     }
109   }
110 
111   private class VTKFrame extends JInternalFrame {
112     private static final long serialVersionUID = 1L;
113 
VTKFrame(int x, int y)114     public VTKFrame(int x, int y) {
115       super("VTK Window", true, true, true, true);
116       Dimension mySize = new Dimension();
117       mySize.height = 300;
118       mySize.width = 300;
119       this.setSize(mySize);
120       this.getContentPane().setLayout(new BorderLayout());
121       this.setLocation(x, y);
122 
123       this.getContentPane().add(new VTKCanvas(), BorderLayout.CENTER);
124       this.pack();
125       this.setVisible(true);
126     }
127   }
128 
129   private class MenuMgr extends JMenuBar {
130     private static final long serialVersionUID = 1L;
131     private JMenu menu;
132 
MenuMgr()133     public MenuMgr() {
134       super();
135       JPopupMenu.setDefaultLightWeightPopupEnabled(false);
136 
137       menu = new JMenu("File");
138       menu.add(new CreateWindowAction("Create New VTK Window"));
139       menu.add(new KillAction("Exit"));
140       add(menu);
141 
142       addMenuBar(this);
143     }
144   }
145 
146   private class CreateWindowAction extends AbstractAction {
147     private static final long serialVersionUID = 1L;
148     private int layer = 0;
149 
CreateWindowAction(String label)150     public CreateWindowAction(String label) {
151       super(label);
152     }
153 
actionPerformed(ActionEvent ev)154     public void actionPerformed(ActionEvent ev) {
155       theDesktop.add(new VTKFrame(340, 200), new Integer(layer));
156     }
157   }
158 
159   private class KillAction extends AbstractAction {
160     private static final long serialVersionUID = 1L;
161 
KillAction(String label)162     public KillAction(String label) {
163       super(label);
164     }
165 
actionPerformed(ActionEvent ev)166     public void actionPerformed(ActionEvent ev) {
167       System.exit(0);
168     }
169   }
170 
main(String[] args)171   public static void main(String[] args) {
172     SwingUtilities.invokeLater(new Runnable() {
173       public void run() {
174         new InternalFrames();
175       }
176     });
177   }
178 }
179