[77] | 1 | package com.framsticks.gui; |
---|
| 2 | |
---|
| 3 | import com.framsticks.core.*; |
---|
[97] | 4 | import com.framsticks.gui.console.Console; |
---|
| 5 | import com.framsticks.gui.console.TrackConsole; |
---|
[88] | 6 | import com.framsticks.params.annotations.AutoAppendAnnotation; |
---|
| 7 | import com.framsticks.params.annotations.FramsClassAnnotation; |
---|
| 8 | import com.framsticks.params.annotations.ParamAnnotation; |
---|
[97] | 9 | import com.framsticks.remote.RemoteTree; |
---|
| 10 | import com.framsticks.util.FramsticksException; |
---|
[88] | 11 | import com.framsticks.util.dispatching.AbstractJoinable; |
---|
[84] | 12 | import com.framsticks.util.dispatching.Dispatcher; |
---|
[88] | 13 | import com.framsticks.util.dispatching.Dispatching; |
---|
[97] | 14 | import com.framsticks.util.dispatching.ExceptionResultHandler; |
---|
[84] | 15 | import com.framsticks.util.dispatching.Future; |
---|
[88] | 16 | import com.framsticks.util.dispatching.Joinable; |
---|
| 17 | import com.framsticks.util.dispatching.JoinableCollection; |
---|
| 18 | import com.framsticks.util.dispatching.JoinableParent; |
---|
| 19 | import com.framsticks.util.dispatching.JoinableState; |
---|
[84] | 20 | |
---|
[88] | 21 | import javax.swing.*; |
---|
| 22 | |
---|
[77] | 23 | import org.apache.log4j.Logger; |
---|
| 24 | |
---|
[84] | 25 | import java.awt.Dimension; |
---|
[97] | 26 | import java.awt.Toolkit; |
---|
| 27 | import java.awt.datatransfer.StringSelection; |
---|
| 28 | import java.awt.event.ActionEvent; |
---|
[84] | 29 | import java.util.ArrayList; |
---|
[97] | 30 | import java.util.LinkedList; |
---|
[84] | 31 | import java.util.List; |
---|
[85] | 32 | import com.framsticks.util.dispatching.RunAt; |
---|
[77] | 33 | |
---|
| 34 | /** |
---|
| 35 | * @author Piotr Sniegowski |
---|
| 36 | */ |
---|
[88] | 37 | @FramsClassAnnotation |
---|
[97] | 38 | public class Browser extends AbstractJoinable implements Dispatcher<Browser>, JoinableParent, ExceptionResultHandler { |
---|
[77] | 39 | |
---|
[97] | 40 | private static final Logger log = Logger.getLogger(Browser.class); |
---|
[77] | 41 | |
---|
[97] | 42 | protected final JoinableCollection<Frame> frames = new JoinableCollection<Frame>().setObservableName("frames"); |
---|
| 43 | protected final JoinableCollection<Tree> trees = new JoinableCollection<Tree>().setObservableName("trees"); |
---|
| 44 | protected final JoinableCollection<Console> consoles = new JoinableCollection<Console>().setObservableName("consoles"); |
---|
[88] | 45 | |
---|
[97] | 46 | protected final List<PopupMenuEntryProvider> popupMenuEntryProviders = new LinkedList<>(); |
---|
[98] | 47 | // protected final SwingDispatcher |
---|
[97] | 48 | |
---|
| 49 | protected final MainFrame mainFrame; |
---|
| 50 | protected final List<PanelProvider> panelProviders = new ArrayList<PanelProvider>(); |
---|
[84] | 51 | protected Dimension defaultFrameDimension; |
---|
[77] | 52 | |
---|
[88] | 53 | String name; |
---|
| 54 | |
---|
[84] | 55 | public void addFrame(Frame frame) { |
---|
| 56 | frames.add(frame); |
---|
| 57 | } |
---|
[77] | 58 | |
---|
[84] | 59 | public Browser() { |
---|
[88] | 60 | setName("browser"); |
---|
[84] | 61 | JPopupMenu.setDefaultLightWeightPopupEnabled(false); |
---|
| 62 | addPanelProvider(new StandardPanelProvider()); |
---|
[77] | 63 | |
---|
[88] | 64 | mainFrame = new MainFrame(Browser.this); |
---|
[98] | 65 | |
---|
| 66 | // mainFrame.getStatusBar().setExceptionHandler(ThrowExceptionHandler.getInstance()); |
---|
| 67 | |
---|
[88] | 68 | addFrame(mainFrame); |
---|
[97] | 69 | |
---|
| 70 | addPopupMenuEntryProvider(new PopupMenuEntryProvider() { |
---|
| 71 | @Override |
---|
| 72 | public void provide(JPopupMenu menu, Path path) { |
---|
| 73 | menu.add(new JMenuItem(path.getFullTextual())); |
---|
| 74 | menu.addSeparator(); |
---|
| 75 | } |
---|
| 76 | }); |
---|
| 77 | |
---|
| 78 | addPopupMenuEntryProvider(new PopupMenuEntryProvider() { |
---|
| 79 | @SuppressWarnings("serial") |
---|
| 80 | @Override |
---|
| 81 | public void provide(JPopupMenu menu, final Path path) { |
---|
| 82 | menu.add(new AbstractAction("Copy path to clipboard") { |
---|
| 83 | @Override |
---|
| 84 | public void actionPerformed(ActionEvent e) { |
---|
| 85 | Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(path.getFullTextual()), null); |
---|
| 86 | } |
---|
| 87 | }); |
---|
| 88 | } |
---|
| 89 | }); |
---|
| 90 | |
---|
| 91 | addPopupMenuEntryProvider(new PopupMenuEntryProvider() { |
---|
| 92 | @SuppressWarnings("serial") |
---|
| 93 | @Override |
---|
| 94 | public void provide(JPopupMenu menu, final Path path) { |
---|
| 95 | if (!(path.getTree() instanceof RemoteTree)) { |
---|
| 96 | return; |
---|
| 97 | } |
---|
| 98 | final RemoteTree remoteTree = (RemoteTree) path.getTree(); |
---|
| 99 | menu.add(new AbstractAction("Open tracking console") { |
---|
| 100 | @Override |
---|
| 101 | public void actionPerformed(ActionEvent e) { |
---|
| 102 | consoles.add(new TrackConsole().setConnection(remoteTree.getConnection())); |
---|
| 103 | } |
---|
| 104 | }); |
---|
| 105 | } |
---|
| 106 | }); |
---|
| 107 | // addNodeActionToTreePopupMenu("", new NodeAction() ) |
---|
| 108 | |
---|
[77] | 109 | } |
---|
| 110 | |
---|
[88] | 111 | @AutoAppendAnnotation |
---|
[84] | 112 | public void addPanelProvider(PanelProvider panelProvider) { |
---|
| 113 | log.debug("added panel provider of type: " + panelProvider.getClass().getCanonicalName()); |
---|
| 114 | panelProviders.add(panelProvider); |
---|
| 115 | } |
---|
| 116 | |
---|
[88] | 117 | @AutoAppendAnnotation |
---|
[97] | 118 | public void addPopupMenuEntryProvider(PopupMenuEntryProvider popupMenuEntryProvider) { |
---|
| 119 | popupMenuEntryProviders.add(popupMenuEntryProvider); |
---|
[88] | 120 | } |
---|
| 121 | |
---|
[97] | 122 | @AutoAppendAnnotation |
---|
| 123 | public void addTree(Tree tree) { |
---|
| 124 | log.info("adding tree: " + tree); |
---|
[98] | 125 | tree.setDispatcher(new SwingDispatcher<Tree>()); |
---|
[97] | 126 | trees.add(tree); |
---|
| 127 | } |
---|
| 128 | |
---|
[84] | 129 | public void autoResolvePath(final String path, final Future<Path> future) { |
---|
[98] | 130 | // final Tree i = trees.get("localhost"); |
---|
| 131 | // i.dispatch(new RunAt<Tree>(future) { |
---|
| 132 | // @Override |
---|
| 133 | // protected void runAt() { |
---|
| 134 | // TreeOperations.tryGet(i, path, new FutureHandler<Path>(future) { |
---|
| 135 | // @Override |
---|
| 136 | // protected void result(final Path p) { |
---|
| 137 | // future.pass(p); |
---|
| 138 | // mainFrame.dispatch(new RunAt<Frame>(future) { |
---|
| 139 | // @Override |
---|
| 140 | // protected void runAt() { |
---|
| 141 | // mainFrame.goTo(p); |
---|
| 142 | // } |
---|
| 143 | // }); |
---|
| 144 | // } |
---|
| 145 | // }); |
---|
| 146 | // } |
---|
| 147 | // }); |
---|
[84] | 148 | } |
---|
| 149 | |
---|
[77] | 150 | public void clear() { |
---|
[84] | 151 | assert isActive(); |
---|
| 152 | for (Frame f : frames) { |
---|
| 153 | f.clear(); |
---|
| 154 | } |
---|
[77] | 155 | } |
---|
| 156 | |
---|
[84] | 157 | public void createTreeNodeForChild(final Path path) { |
---|
| 158 | assert !isActive(); |
---|
[97] | 159 | //assert tree.isActive(); |
---|
[77] | 160 | |
---|
[88] | 161 | /* |
---|
| 162 | final TreeNode parentTreeNode = (TreeNode) child.getParent().getUserObject(); |
---|
| 163 | if (parentTreeNode == null) { |
---|
| 164 | Dispatching.invokeDispatch(this, manager, new Runnable() { |
---|
| 165 | @Override |
---|
| 166 | public void run() { |
---|
| 167 | createTreeNodeForChild(child); |
---|
| 168 | } |
---|
| 169 | }); |
---|
| 170 | return; |
---|
| 171 | } |
---|
| 172 | log.debug(child.getClass().getSimpleName() + " created: " + child); |
---|
[77] | 173 | |
---|
| 174 | |
---|
[88] | 175 | invokeLater(new Runnable() { |
---|
| 176 | @Override |
---|
| 177 | public void run() { |
---|
| 178 | parentTreeNode.getOrCreateChildTreeNodeFor(child); |
---|
| 179 | } |
---|
| 180 | }); |
---|
| 181 | */ |
---|
| 182 | } |
---|
[77] | 183 | |
---|
[88] | 184 | @Override |
---|
| 185 | protected void joinableStart() { |
---|
| 186 | Dispatching.use(frames, this); |
---|
[97] | 187 | Dispatching.use(trees, this); |
---|
| 188 | Dispatching.use(consoles, this); |
---|
[88] | 189 | |
---|
[97] | 190 | dispatch(new RunAt<Browser>(this) { |
---|
[84] | 191 | @Override |
---|
[97] | 192 | protected void runAt() { |
---|
| 193 | |
---|
| 194 | for (final Tree i : trees) { |
---|
| 195 | i.dispatch(new RunAt<Tree>(this) { |
---|
| 196 | @Override |
---|
| 197 | protected void runAt() { |
---|
| 198 | final Path p = Path.to(i, "/"); |
---|
| 199 | dispatch(new RunAt<Browser>(this) { |
---|
| 200 | @Override |
---|
| 201 | protected void runAt() { |
---|
| 202 | mainFrame.addRootPath(p); |
---|
| 203 | } |
---|
| 204 | }); |
---|
| 205 | } |
---|
| 206 | }); |
---|
| 207 | } |
---|
[84] | 208 | } |
---|
| 209 | }); |
---|
[77] | 210 | } |
---|
| 211 | |
---|
[88] | 212 | /** |
---|
[97] | 213 | * @return the tree |
---|
[88] | 214 | */ |
---|
[97] | 215 | public JoinableCollection<Tree> getTrees() { |
---|
| 216 | return trees; |
---|
[84] | 217 | } |
---|
[77] | 218 | |
---|
[84] | 219 | /** |
---|
| 220 | * @return the mainFrame |
---|
| 221 | */ |
---|
| 222 | public MainFrame getMainFrame() { |
---|
| 223 | return mainFrame; |
---|
| 224 | } |
---|
| 225 | |
---|
[88] | 226 | /** |
---|
| 227 | * @return the name |
---|
| 228 | */ |
---|
| 229 | @ParamAnnotation |
---|
| 230 | public String getName() { |
---|
| 231 | return name; |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | /** |
---|
| 235 | * @param name the name to set |
---|
| 236 | */ |
---|
| 237 | @ParamAnnotation |
---|
| 238 | public void setName(String name) { |
---|
| 239 | this.name = name; |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | @Override |
---|
| 243 | public boolean isActive() { |
---|
| 244 | return SwingDispatcher.getInstance().isActive(); |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | @Override |
---|
[90] | 248 | public void dispatch(RunAt<? extends Browser> runnable) { |
---|
| 249 | SwingDispatcher.getInstance().dispatch(runnable); |
---|
[88] | 250 | } |
---|
| 251 | |
---|
| 252 | @Override |
---|
| 253 | protected void joinableJoin() throws InterruptedException { |
---|
| 254 | Dispatching.join(frames); |
---|
[97] | 255 | Dispatching.join(trees); |
---|
| 256 | Dispatching.join(consoles); |
---|
[88] | 257 | // super.join(); |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | @Override |
---|
| 261 | protected void joinableInterrupt() { |
---|
[97] | 262 | Dispatching.drop(consoles, this); |
---|
[88] | 263 | Dispatching.drop(frames, this); |
---|
[97] | 264 | Dispatching.drop(trees, this); |
---|
[88] | 265 | } |
---|
| 266 | |
---|
| 267 | @Override |
---|
| 268 | public void childChangedState(Joinable joinable, JoinableState state) { |
---|
| 269 | if (joinable == frames) { |
---|
| 270 | proceedToState(state); |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | @Override |
---|
| 276 | protected void joinableFinish() { |
---|
| 277 | |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | @Override |
---|
| 281 | public String toString() { |
---|
| 282 | return getName(); |
---|
| 283 | } |
---|
| 284 | |
---|
[97] | 285 | @Override |
---|
| 286 | public void handle(FramsticksException exception) { |
---|
| 287 | mainFrame.handle(exception); |
---|
| 288 | } |
---|
[88] | 289 | |
---|
[98] | 290 | // final protected Map<EventParam, Subscription<?>> userSubscriptions = new HashMap<>(); |
---|
| 291 | // public boolean hasSubscribed(EventParam param) { |
---|
| 292 | // assert frame.isActive(); |
---|
| 293 | // return userSubscriptions.containsKey(param); |
---|
[88] | 294 | // } |
---|
[98] | 295 | |
---|
| 296 | // public void unsubscribe(EventParam eventParam) { |
---|
| 297 | // assert frame.isActive(); |
---|
| 298 | // if (!hasSubscribed(eventParam)) { |
---|
| 299 | // log.error("could not unsubscribe from " + eventParam); |
---|
| 300 | // return; |
---|
| 301 | // } |
---|
| 302 | // userSubscriptions.get(eventParam).unsubscribe(new LoggingStateCallback(log, "unsubscribed " + eventParam)); |
---|
| 303 | // userSubscriptions.remove(eventParam); |
---|
| 304 | // } |
---|
| 305 | |
---|
| 306 | |
---|
| 307 | |
---|
| 308 | |
---|
[77] | 309 | } |
---|