~krause/xabsleditor/master

« back to all changes in this revision

Viewing changes to src/de/naoth/xabsleditor/editorpanel/EditorPanel.java

  • Committer: GitHub
  • Author(s): Yigit Akcay
  • Date: 2018-05-18 15:41:26 UTC
  • mfrom: (185.1.26)
  • Revision ID: git-v1:4f4ec3a2838097c734c228cdb4b0c87881c2de50
Merge pull request #15 from BerlinUnited/feature/event_based_architecture

event based architecture

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
import de.naoth.xabsleditor.FileDrop;
4
4
import de.naoth.xabsleditor.Main;
5
 
import de.naoth.xabsleditor.Tools;
6
 
import de.naoth.xabsleditor.graphpanel.GraphPanel;
 
5
import de.naoth.xabsleditor.events.EventManager;
 
6
import de.naoth.xabsleditor.events.LocateFileEvent;
 
7
import de.naoth.xabsleditor.events.RefreshGraphEvent;
7
8
import de.naoth.xabsleditor.parser.XABSLContext;
8
 
import de.naoth.xabsleditor.parser.XParser;
9
9
import de.naoth.xabsleditor.utils.FileWatcher;
10
 
import java.awt.AWTKeyStroke;
11
10
import java.awt.Component;
12
11
import java.awt.Container;
13
 
import java.awt.KeyboardFocusManager;
14
 
import java.awt.event.KeyEvent;
15
 
import java.awt.event.KeyListener;
16
12
import java.awt.event.MouseEvent;
17
13
import java.awt.event.MouseListener;
18
14
import java.io.File;
19
 
import java.io.FileReader;
20
15
import java.util.ArrayList;
21
 
import java.util.HashSet;
 
16
import java.util.Enumeration;
22
17
import java.util.Iterator;
23
 
import java.util.Set;
 
18
import java.util.List;
24
19
import javax.swing.InputMap;
25
20
import javax.swing.JComponent;
26
21
import javax.swing.JOptionPane;
 
22
import javax.swing.JTabbedPane;
27
23
import javax.swing.KeyStroke;
28
24
import javax.swing.event.ChangeEvent;
 
25
import javax.swing.plaf.TabbedPaneUI;
 
26
import javax.swing.tree.DefaultMutableTreeNode;
29
27
import org.fife.ui.autocomplete.DefaultCompletionProvider;
30
28
import org.fife.ui.autocomplete.ShorthandCompletion;
31
29
 
35
33
 */
36
34
public class EditorPanel extends javax.swing.JPanel implements Iterable<EditorPanelTab>
37
35
{
38
 
    private GraphPanel graph;
 
36
    /** Manager for distributing events. */
 
37
    EventManager evtManager = EventManager.getInstance();
 
38
 
39
39
    private FileWatcher watcher;
40
40
    private EditorPanelTab activeTab = null;
41
41
    
48
48
     */
49
49
    public EditorPanel() {
50
50
        initComponents();
 
51
        // register event handler
 
52
        evtManager.add(this);
51
53
        // add "tab-switch" listener
52
54
        tabs.addChangeListener((ChangeEvent e) -> {
53
55
            activeTab = (EditorPanelTab) tabs.getSelectedComponent();
54
56
            if(activeTab != null) {
55
57
                activeTab.select();
 
58
                evtManager.publish(new RefreshGraphEvent(activeTab));
56
59
            }
57
 
            graph.refreshGraph();
58
60
        });
 
61
        tabs.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
59
62
 
60
63
        // add right-click-behavior of the tabs
61
64
        tabs.addMouseListener(new MouseListener() {
107
110
        tabPopupMenu_Close = new javax.swing.JMenuItem();
108
111
        tabPopupMenu_CloseAll = new javax.swing.JMenuItem();
109
112
        tabPopupMenu_CloseOthers = new javax.swing.JMenuItem();
 
113
        tabPopupMenu_Sep = new javax.swing.JPopupMenu.Separator();
 
114
        tabPopupMenu_Locate = new javax.swing.JMenuItem();
110
115
        tabs = new javax.swing.JTabbedPane();
111
116
 
112
117
        tabPopupMenu_Close.setText("Close");
137
142
            }
138
143
        });
139
144
        tabPopupMenu.add(tabPopupMenu_CloseOthers);
 
145
        tabPopupMenu.add(tabPopupMenu_Sep);
 
146
 
 
147
        tabPopupMenu_Locate.setText("Locate file in project tree");
 
148
        tabPopupMenu_Locate.addActionListener(new java.awt.event.ActionListener() {
 
149
            public void actionPerformed(java.awt.event.ActionEvent evt) {
 
150
                tabPopupMenu_LocateActionPerformed(evt);
 
151
            }
 
152
        });
 
153
        tabPopupMenu.add(tabPopupMenu_Locate);
140
154
 
141
155
        setLayout(new java.awt.BorderLayout());
142
156
        add(tabs, java.awt.BorderLayout.CENTER);
156
170
        }
157
171
    }//GEN-LAST:event_tabPopupMenu_CloseOthersActionPerformed
158
172
 
159
 
    public void setGraph(GraphPanel g) {
160
 
        graph = g;
161
 
    }
162
 
    
 
173
    private void tabPopupMenu_LocateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tabPopupMenu_LocateActionPerformed
 
174
        if(activeTab != null) {
 
175
            locateTabInProjectTree(activeTab);
 
176
        }
 
177
    }//GEN-LAST:event_tabPopupMenu_LocateActionPerformed
 
178
 
163
179
    public void setTabSize(int size) {
164
180
        tabSize = size;
165
181
        for (EditorPanelTab tab : this) {
171
187
        return tabSize;
172
188
    }
173
189
    
 
190
    public void setTabLayout(int layout) {
 
191
        tabs.setTabLayoutPolicy(layout);
 
192
    }
 
193
    
 
194
    public int getTabLayout() {
 
195
        return tabs.getTabLayoutPolicy();
 
196
    }
 
197
    
 
198
    public void setTabUI(TabbedPaneUI ui) {
 
199
        tabs.setUI(ui);
 
200
    }
 
201
    
 
202
    public TabbedPaneUI getTabUI() {
 
203
        return tabs.getUI();
 
204
    }
 
205
    
174
206
    public void setFontSize(float size) {
175
207
        fontSize = size;
176
208
        for (EditorPanelTab tab : this) {
195
227
    public boolean getShowCloseButtons() {
196
228
        return showCloseButtons;
197
229
    }
198
 
    
199
 
    public void openFile(File f) {
200
 
        if (f == null) {
 
230
 
 
231
    public void openFile(File file, File agent, XABSLContext context, int carret, String search) {
 
232
        if (file == null) {
201
233
            createDocumentTab(null, null, null);
202
234
        } else {
203
235
            // find and select already opened file
204
236
            for (EditorPanelTab tab : this) {
205
 
                if(tab.getFile() != null && tab.getFile().equals(f)) {
 
237
                if(tab.getFile() != null && tab.getFile().equals(file)) {
206
238
                    tabs.setSelectedComponent(tab);
207
239
                    return;
208
240
                }
209
241
            }
210
242
            // ... otherwise create new tab
211
 
            File agentsFile = Tools.getAgentFileForOption(f);
212
 
            XABSLContext newContext = null;
213
 
 
214
 
            if (agentsFile != null) {
215
 
                newContext = loadXABSLContext(agentsFile.getParentFile(), null);
216
 
            }
217
 
 
218
 
            createDocumentTab(f, newContext, agentsFile);
219
 
        }
220
 
    }
221
 
    
222
 
    public void openFile(File f, int position) {
223
 
        openFile(f);
224
 
        activeTab.setCarretPosition(position);
225
 
    }
226
 
 
227
 
    public XABSLContext loadXABSLContext(File folder, XABSLContext context) {
228
 
        if (context == null) {
229
 
            context = new XABSLContext();
230
 
        }
231
 
 
232
 
        final String XABSL_FILE_ENDING = ".xabsl";
233
 
 
234
 
        File[] fileList = folder.listFiles();
235
 
        for (File file : fileList) {
236
 
            if (file.isDirectory()) {
237
 
                loadXABSLContext(file, context);
238
 
            } else if (file.getName().toLowerCase().endsWith(XABSL_FILE_ENDING)) {
239
 
                // remove the file ending
240
 
                int dotIndex = file.getName().length() - XABSL_FILE_ENDING.length();
241
 
                String name = file.getName().substring(0, dotIndex);
242
 
                context.getOptionPathMap().put(name, file);
243
 
 
244
 
                // parse XABSL file
245
 
                try {
246
 
                    //System.out.println("parse: " + file.getName()); // debug stuff
247
 
                    XParser p = new XParser(context);
248
 
                    p.parse(new FileReader(file), file.getAbsolutePath());
249
 
 
250
 
                    // HACK: problems with equal file names
251
 
                    context.getFileTypeMap().put(name, p.getFileType());
252
 
                } catch (Exception e) {
253
 
                    System.err.println("Couldn't read the XABSL file " + file.getAbsolutePath());
254
 
                }
255
 
            }
256
 
        }//end for
257
 
 
258
 
        return context;
259
 
    }//end loadXABSLContext
 
243
            createDocumentTab(file, context, agent);
 
244
            
 
245
            if(search == null) {
 
246
                activeTab.setCarretPosition(carret);
 
247
            } else {
 
248
                activeTab.search(search);
 
249
            }
 
250
        }
 
251
    }
260
252
 
261
253
    private EditorPanelTab createDocumentTab(File file, XABSLContext context, File agentsFile) {
262
254
        try {
387
379
        }
388
380
    }
389
381
    
 
382
    public void locateTabInProjectTree(EditorPanelTab t) {
 
383
        evtManager.publish(new LocateFileEvent(this, t.getFile()));
 
384
    }
 
385
    
 
386
    private final List<DefaultMutableTreeNode> getSearchNodes(DefaultMutableTreeNode root) {
 
387
            List<DefaultMutableTreeNode> searchNodes = new ArrayList<DefaultMutableTreeNode>();
 
388
 
 
389
            Enumeration<?> e = root.preorderEnumeration();
 
390
            while(e.hasMoreElements()) {
 
391
                searchNodes.add((DefaultMutableTreeNode)e.nextElement());
 
392
            }
 
393
            return searchNodes;
 
394
        }
 
395
    
390
396
    public void save() {
391
397
        save(System.getProperty("user.home"));
392
398
    }
393
399
    
394
400
    public void save(String defaultDirectory) {
395
401
        if(activeTab != null && activeTab.save(defaultDirectory)) {
396
 
            graph.refreshGraph();
 
402
            evtManager.publish(new RefreshGraphEvent(activeTab));
397
403
        }
398
404
    }
399
405
 
406
412
            File old = activeTab.getFile();
407
413
            activeTab.setFile(null);
408
414
            if(activeTab.save(defaultDirectory)) {
409
 
                graph.refreshGraph();
 
415
                evtManager.publish(new RefreshGraphEvent(activeTab));
410
416
            } else {
411
417
                activeTab.setFile(old);
412
418
            }
417
423
        return tabs.getTabCount() > 0;
418
424
    }
419
425
    
 
426
    public ArrayList<EditorPanelTab> hasOpenUnsavedFiles() {
 
427
        ArrayList<EditorPanelTab> unsavedtabs = new ArrayList<>();
 
428
        for (EditorPanelTab tab : this) {
 
429
            if(tab.isChanged() && tab.getFile() != null) {
 
430
                unsavedtabs.add(tab);
 
431
            }
 
432
        }
 
433
        return unsavedtabs;
 
434
    }
 
435
    
420
436
    public ArrayList<File> getOpenFiles() {
421
437
        ArrayList<File> files = new ArrayList<>();
422
438
        for (EditorPanelTab tab : this) {
469
485
    private javax.swing.JMenuItem tabPopupMenu_Close;
470
486
    private javax.swing.JMenuItem tabPopupMenu_CloseAll;
471
487
    private javax.swing.JMenuItem tabPopupMenu_CloseOthers;
 
488
    private javax.swing.JMenuItem tabPopupMenu_Locate;
 
489
    private javax.swing.JPopupMenu.Separator tabPopupMenu_Sep;
472
490
    private javax.swing.JTabbedPane tabs;
473
491
    // End of variables declaration//GEN-END:variables
474
492