1 54 55 56 package swingwtx.swing; 57 58 import java.util.Iterator ; 59 import java.util.Vector ; 60 61 import org.eclipse.swt.widgets.Control; 62 63 import swingwt.awt.BorderLayout; 64 import swingwt.awt.Component; 65 import swingwtx.custom.JClosableTabbedPane; 66 import swingwtx.custom.event.TabCloseListener; 67 68 public class JDesktopPane extends JComponent { 69 70 protected org.eclipse.swt.widgets.Composite ppeer = null; 71 protected Vector frames = new Vector (); 72 protected JClosableTabbedPane jtp = null; 73 74 public JDesktopPane() {} 75 76 public Control getPeer() { return ppeer; } 77 78 79 public Component add(final Component c) { 80 81 if (!(c instanceof JInternalFrame)) { 82 doAdd(c); 83 return c; 84 } 85 else 86 return (Component) addInternalFrame((JInternalFrame) c); 87 } 88 89 90 public void add(Component c, Object layoutModifier) { 91 add(c); 92 } 93 94 95 public Component add(String name, Component c) { 96 c.setName(name); 97 return add(c); 98 } 99 100 101 protected JInternalFrame addInternalFrame(final JInternalFrame frame) { 102 frames.add(frame); 103 frame.setParentPane(this); 104 if (jtp == null) return frame; 105 106 final JDesktopPane me = this; 107 SwingUtilities.invokeSync(new Runnable () { 108 public void run() { 109 if (jtp != null) { 110 jtp.addTab(frame.getTitle(), frame.getFrameIcon(), frame, frame.getTitle()); 111 jtp.setSelectedIndex(jtp.getTabCount() - 1); 113 } 114 } 115 }); 116 return frame; 117 } 118 119 public void setSwingWTParent(swingwt.awt.Container parent) throws Exception { 120 descendantHasPeer = true; 121 ppeer = new org.eclipse.swt.widgets.Composite(parent.getComposite(), 0); 122 peer = ppeer; 123 composite = ppeer; 124 this.parent = parent; 125 super.setSwingWTParent(parent); 126 127 setLayout(new BorderLayout()); 130 jtp = new JClosableTabbedPane(); 131 jtp.setTabPlacement(SwingConstants.TOP); 132 add(jtp, BorderLayout.CENTER); 133 134 Iterator i = frames.iterator(); 135 while (i.hasNext()) { 136 JInternalFrame frame = (JInternalFrame) i.next(); 137 jtp.addTab(frame.getTitle(), frame.getFrameIcon(), frame, frame.getTitle()); 138 } 139 140 jtp.addTabCloseListener(new TabCloseListener() { 142 public boolean tabClosed(int index) { 143 return ((JInternalFrame) frames.get(index)).processFrameClosing(); 144 } 145 }); 146 } 147 148 151 protected void refreshFrame(final JInternalFrame frame) { 152 153 if (frame == null) return; 155 156 SwingUtilities.invokeSync(new Runnable () { 159 public void run() { 160 161 final JInternalFrame[] frames = getAllFrames(); 163 if (frames == null) return; 164 165 for (int i = 0; i < frames.length; i++) 166 if (frames[i] == frame) 167 updateFrameAt(i, frame); 168 } 169 }); 170 } 171 172 175 protected void updateFrameAt(int index, JInternalFrame frame) { 176 jtp.setTitleAt(index, frame.getTitle()); 177 jtp.setIconAt(index, frame.getFrameIcon()); 178 } 179 180 181 protected void removeFrame(JInternalFrame frame) { 182 int i = frames.indexOf(frame); 183 if (i != -1) { 184 jtp.removeTabAt(i); 185 frames.remove(frame); 186 } 187 } 188 189 public JInternalFrame[] getAllFrames() { 190 JInternalFrame[] ret = new JInternalFrame[frames.size()]; 191 Object [] jf = frames.toArray(); 192 for (int i = 0; i < jf.length; i ++) { 193 ret[i] = (JInternalFrame) jf[i]; 194 } 195 jf = null; 196 return ret; 197 } 198 199 public JInternalFrame getSelectedFrame() { 200 if (jtp == null) return null; 201 return (JInternalFrame) frames.get(jtp.getSelectedIndex()); 202 } 203 204 public void setSelectedFrame(JInternalFrame frame) { 205 if (jtp == null) return; 206 int i = frames.indexOf(frame); 207 if (i != -1) 208 jtp.setSelectedIndex(i); 209 } 210 211 } 212 | Popular Tags |