001: package Schmortopf.JavaSourceEditor.SituationalAwareness;
002:
003: /**
004: * The situational awareness popup
005: */
006:
007: import java.awt.*;
008: import java.awt.event.*;
009: import javax.swing.*;
010:
011: import Schmortopf.Utility.gui.JSmartPopupMenu;
012: import Schmortopf.Main.IDE_ProjectFrame;
013: import Schmortopf.ProjectFiles.ProjectFilesTree.ProjectFilesTree;
014: import Schmortopf.FileStructure.Descriptions.*;
015: import Schmortopf.JavaSourceEditor.*;
016: import Schmortopf.Utility.gui.*;
017: import Schmortopf.Utility.ThreadEngine.ThreadEngine;
018: import Language.Language;
019: import Shared.Logging.Log;
020:
021: public class SAPopup extends JSmartPopupMenu {
022:
023: private IDE_ProjectFrame projectFrame;
024: private SituationalAwarenessManager situationalAwarenessManager;
025: private int xMouseCoord;
026: private int yMouseCoord;
027: private JPanel methodInfoPanel;
028: private int editorLineIndex;
029: private SASearchResult searchResult;
030: private SourceEditorDocument sourceDocument;
031: private EditorPanel editorPanel;
032:
033: public SAPopup(
034: final IDE_ProjectFrame theProjectFrame,
035: final SituationalAwarenessManager theSituationalAwarenessManager,
036: final int thexMouseCoord, final int theyMouseCoord,
037: final JPanel theMethodInfoPanel,
038: final int theEditorLineIndex,
039: final SASearchResult theSearchResult,
040: final SourceEditorDocument theSourceDocument,
041: final EditorPanel theEditorPanel) {
042: this .projectFrame = theProjectFrame;
043: this .situationalAwarenessManager = theSituationalAwarenessManager;
044: this .xMouseCoord = thexMouseCoord;
045: this .yMouseCoord = theyMouseCoord;
046: this .methodInfoPanel = theMethodInfoPanel;
047: this .editorLineIndex = theEditorLineIndex;
048: this .searchResult = theSearchResult;
049: this .sourceDocument = theSourceDocument;
050: this .editorPanel = theEditorPanel;
051: } // Constructor
052:
053: public void showPopup() {
054: int offset = UIManager.getFont("Menu.font").getSize();
055: int xp = xMouseCoord + offset / 8;
056: int yp = yMouseCoord - offset;
057: this .setLocation(xp, yp); // prevents a flicker in the screen origin
058: // methodInfoPanel can be null, in which case only the
059: // selection of existing bookmarks is displayed.
060: if (methodInfoPanel != null) {
061: TitlePanel titlePanel = new TitlePanel(methodInfoPanel);
062: this .add(titlePanel);
063: JCreateBookmarkEntry createBookMarkItem = new JCreateBookmarkEntry(
064: Language.Translate("Create a bookmark here"),
065: projectFrame);
066: createBookMarkItem.addActionListener(new ActionListener() {
067: public void actionPerformed(ActionEvent e) {
068: createNewBookMark(sourceDocument, editorLineIndex,
069: searchResult);
070: // Close this popup:
071: setVisible(false);
072: }
073: });
074: this .add(createBookMarkItem);
075: }
076: // Remove bookmarks, which do not exist (= point to a method, which doesn't exist) :
077: this .situationalAwarenessManager.removeNonExistentBookmarks();
078: // Get the existing ones:
079: BookMark[] bookmarks = this .situationalAwarenessManager
080: .getBookMarks();
081: if (bookmarks.length == 0) {
082: TitlePanel bookmarkTitlePanel = new TitlePanel(" "
083: + Language.Translate("Bookmarks"));
084: this .add(bookmarkTitlePanel);
085: } else {
086: TitlePanel bookmarkTitlePanel = new TitlePanel(Language
087: .Translate("Jump to bookmark in"));
088: this .add(bookmarkTitlePanel);
089: }
090: if (bookmarks.length == 0) {
091: JMenuItem noBookMarkItem1 = new JMenuItem(Language
092: .Translate("No bookmarks created by you."));
093: noBookMarkItem1.setEnabled(false);
094: this .add(noBookMarkItem1);
095: if (methodInfoPanel == null) {
096: JMenuItem noBookMarkItem2 = new JMenuItem(
097: Language
098: .Translate("Click red,green or blue regions for creating new ones."));
099: noBookMarkItem2.setEnabled(false);
100: this .add(noBookMarkItem2);
101: }
102: } else {
103: JBookmarkMenuEntry bookmarkMenuEntry;
104: for (int i = 0; i < bookmarks.length; i++) {
105: final int bookmarkIndex = i;
106: final BookMark this BookMark = bookmarks[i];
107: bookmarkMenuEntry = new JBookmarkMenuEntry(this BookMark
108: .getDescription(), projectFrame);
109: bookmarkMenuEntry
110: .setEntryButtonReaction(new ActionListener() {
111: public void actionPerformed(ActionEvent e) {
112: // Close this popup:
113: setVisible(false);
114: EventQueue.invokeLater(new Runnable() {
115: public void run() {
116: // One must leave the EDT, otherwise some visual areas
117: // might not be restaured properly:
118: Thread jumpThread = new Thread() {
119: public void run() {
120: // Time for Swing - This is required, otherwise the
121: // popup space isn't restored properly sometimes.
122: try {
123: Thread.sleep(77);
124: } catch (Exception e) {
125: }
126: jumpToBookMark(this BookMark);
127: }
128: };
129: ThreadEngine
130: .getInstance()
131: .addRunnable(
132: jumpThread,
133: "SA Jump Thread");
134: }
135: });
136: }
137: });
138: bookmarkMenuEntry
139: .setMoveUpButtonReaction(new ActionListener() {
140: public void actionPerformed(ActionEvent e) {
141: moveUpBookmarkEntry(bookmarkIndex);
142: }
143: });
144: bookmarkMenuEntry
145: .setMoveDownButtonReaction(new ActionListener() {
146: public void actionPerformed(ActionEvent e) {
147: moveDownBookmarkEntry(bookmarkIndex);
148: }
149: });
150: bookmarkMenuEntry
151: .setRenameButtonReaction(new ActionListener() {
152: public void actionPerformed(ActionEvent e) {
153: renameBookmarkEntry(bookmarkIndex);
154: }
155: });
156: this .add(bookmarkMenuEntry);
157: bookmarkMenuEntry
158: .setRemoveButtonReaction(new ActionListener() {
159: public void actionPerformed(ActionEvent e) {
160: removeBookmarkEntry(bookmarkIndex);
161: }
162: });
163: this .add(bookmarkMenuEntry);
164: }
165: }
166: // coords in system of editorPanel.getEditor()
167: this .show(editorPanel.getEditor(), xMouseCoord, yMouseCoord);
168: } // showPopup
169:
170: private void moveUpBookmarkEntry(final int bookmarkIndex) {
171: this .situationalAwarenessManager
172: .moveUpBookmarkEntry(bookmarkIndex);
173: this .setVisible(false);
174: this .relaunch();
175: }
176:
177: private void moveDownBookmarkEntry(final int bookmarkIndex) {
178: this .situationalAwarenessManager
179: .moveDownBookmarkEntry(bookmarkIndex);
180: this .setVisible(false);
181: this .relaunch();
182: }
183:
184: private void renameBookmarkEntry(final int bookmarkIndex) {
185: this .situationalAwarenessManager
186: .renameBookmarkEntry(bookmarkIndex);
187: this .relaunch();
188: }
189:
190: private void removeBookmarkEntry(final int bookmarkIndex) {
191: this .situationalAwarenessManager
192: .removeBookmarkEntry(bookmarkIndex);
193: this .setVisible(false);
194: this .relaunch();
195: }
196:
197: private void relaunch() {
198: SAPopup aNewOne = new SAPopup(this .projectFrame,
199: this .situationalAwarenessManager, this .xMouseCoord,
200: this .yMouseCoord, this .methodInfoPanel,
201: this .editorLineIndex, this .searchResult,
202: this .sourceDocument, this .editorPanel);
203: aNewOne.showPopup();
204: }
205:
206: private final void createNewBookMark(
207: final SourceEditorDocument sourceDocument,
208: final int editorLineIndex, final SASearchResult searchResult) {
209: situationalAwarenessManager.createNewBookMark(sourceDocument,
210: editorLineIndex, searchResult);
211: } // createNewBookMark
212:
213: private final void jumpToBookMark(final BookMark bookmark) {
214: // Calculate the absolute line number in the editor:
215: final int absoluteEditorLineIndex = this .situationalAwarenessManager
216: .getAbsoluteEditorLineNumberFor(bookmark);
217: if (absoluteEditorLineIndex >= 0) {
218: final ProjectFilesTree projectFilesTree = projectFrame
219: .getProjectFilesManager().getProjectFilesTree();
220: EventQueue.invokeLater(new Runnable() {
221: public void run() {
222: projectFilesTree.showLeafFor(bookmark
223: .getSourceFilePathName(),
224: absoluteEditorLineIndex, 0, false);
225: }
226: });
227: } // if
228: else {
229: // This should not occure, unless the current source has syntax errors, and
230: // the associated fsd therefore isn't complete at this time.
231: if (absoluteEditorLineIndex != this .situationalAwarenessManager.FileStructureDescriptionHasSyntaxErrors) {
232: Log.Warn("No target for bookmark "
233: + bookmark.getMethodSignature()
234: + " in correct sourcefile.");
235: }
236: }
237: } // jumpToBookMark
238:
239: } // SAPopup
|