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