Skip to content

Commit b6e2643

Browse files
committed
Manual code cleanup ConsoleView
- Moved final field inits to constructor - Get rid of unneeded or duplicated code, obsoleted comments - Use instanceof variables - Fix spelling mistakes - Changed all access to fActiveConsole via get/set methods
1 parent 0ababdf commit b6e2643

1 file changed

Lines changed: 71 additions & 64 deletions

File tree

  • debug/org.eclipse.ui.console/src/org/eclipse/ui/internal/console

debug/org.eclipse.ui.console/src/org/eclipse/ui/internal/console/ConsoleView.java

Lines changed: 71 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,17 @@ public class ConsoleView extends PageBookView implements IConsoleView, IConsoleL
8080
/**
8181
* Whether this console is pinned.
8282
*/
83-
private boolean fPinned = false;
83+
private boolean fPinned;
8484

8585
/**
8686
* Stack of consoles in MRU order
8787
*/
88-
private final List<IConsole> fStack = new ArrayList<>();
88+
private final List<IConsole> fStack;
8989

9090
/**
9191
* The console being displayed, or <code>null</code> if none
9292
*/
93-
private IConsole fActiveConsole = null;
93+
private IConsole fActiveConsole;
9494

9595
/**
9696
* Map of consoles to dummy console parts (used to close pages)
@@ -110,18 +110,18 @@ public class ConsoleView extends PageBookView implements IConsoleView, IConsoleL
110110
/**
111111
* Whether this view is active
112112
*/
113-
private boolean fActive = false;
113+
private boolean fActive;
114114

115115
/**
116116
* 'In Console View' context
117117
*/
118118
private IContextActivation fActivatedContext;
119119

120120
// actions
121-
private PinConsoleAction fPinAction = null;
122-
private ConsoleDropDownAction fDisplayConsoleAction = null;
121+
private PinConsoleAction fPinAction;
122+
private ConsoleDropDownAction fDisplayConsoleAction;
123123

124-
private OpenConsoleAction fOpenConsoleAction = null;
124+
private OpenConsoleAction fOpenConsoleAction;
125125

126126
private boolean fScrollLock;
127127
private boolean fWordWrap;
@@ -132,7 +132,8 @@ public class ConsoleView extends PageBookView implements IConsoleView, IConsoleL
132132
private boolean updateConsoleIcon;
133133

134134
private boolean isAvailable() {
135-
return getPageBook() != null && !getPageBook().isDisposed();
135+
PageBook pageBook = getPageBook();
136+
return pageBook != null && !pageBook.isDisposed();
136137
}
137138

138139
@Override
@@ -165,11 +166,16 @@ public IConsole getConsole() {
165166
return fActiveConsole;
166167
}
167168

169+
private void setConsole(IConsole recConsole) {
170+
fActiveConsole = recConsole;
171+
}
172+
168173
@Override
169174
protected void showPageRec(PageRec pageRec) {
175+
IConsole oldActiveConsole = getConsole();
170176
// don't show the page when pinned, unless this is the first console to be added
171177
// or its the default page
172-
if (fActiveConsole != null && pageRec.page != getDefaultPage() && fPinned && fConsoleToPart.size() > 1) {
178+
if (oldActiveConsole != null && pageRec.page != getDefaultPage() && fPinned && fConsoleToPart.size() > 1) {
173179
IConsole console = fPartToConsole.get(pageRec.part);
174180
if (!fStack.contains(console)) {
175181
fStack.add(console);
@@ -178,25 +184,25 @@ protected void showPageRec(PageRec pageRec) {
178184
}
179185

180186
IConsole recConsole = fPartToConsole.get(pageRec.part);
181-
if (recConsole!=null && recConsole.equals(fActiveConsole)) {
187+
if (recConsole != null && recConsole.equals(oldActiveConsole)) {
182188
return;
183189
}
184190

185191
super.showPageRec(pageRec);
186192

187-
if (fActiveConsole != recConsole) {
188-
if (fActive && fActiveConsole != null) {
189-
deactivateParticipants(fActiveConsole);
193+
if (oldActiveConsole != recConsole) {
194+
if (fActive && oldActiveConsole != null) {
195+
deactivateParticipants(oldActiveConsole);
190196
}
191197
if (recConsole != null) {
192198
activateParticipants(recConsole);
193199
}
194200
}
195-
fActiveConsole = recConsole;
201+
setConsole(recConsole);
196202
// bring active console on top of stack
197-
if (fActiveConsole != null && !fStack.isEmpty() && !fActiveConsole.equals(fStack.get(0))) {
198-
fStack.remove(fActiveConsole);
199-
fStack.add(0, fActiveConsole);
203+
if (recConsole != null && !fStack.isEmpty() && !recConsole.equals(fStack.get(0))) {
204+
fStack.remove(recConsole);
205+
fStack.add(0, recConsole);
200206
}
201207
updateTitle();
202208
updateHelp();
@@ -206,17 +212,17 @@ protected void showPageRec(PageRec pageRec) {
206212
fPinAction.update();
207213
}
208214
IPage page = getCurrentPage();
209-
if (page instanceof IOConsolePage) {
210-
((IOConsolePage) page).setWordWrap(fWordWrap);
215+
if (page instanceof IOConsolePage consolePage) {
216+
consolePage.setWordWrap(fWordWrap);
211217
}
212218
/*
213219
* Bug 268608: cannot invoke find/replace after opening console
214220
*
215221
* Global actions of TextConsolePage must be updated here,
216222
* but they are only updated on a selection change.
217223
*/
218-
if (page instanceof TextConsolePage) {
219-
TextConsoleViewer viewer = ((TextConsolePage) page).getViewer();
224+
if (page instanceof TextConsolePage consolePage) {
225+
TextConsoleViewer viewer = consolePage.getViewer();
220226
viewer.setSelection(viewer.getSelection());
221227
}
222228
}
@@ -268,7 +274,7 @@ protected void updateTitle() {
268274
} else {
269275
String newName = console.getName();
270276
String oldName = getContentDescription();
271-
if (newName!=null && !(newName.equals(oldName))) {
277+
if (newName != null && !(newName.equals(oldName))) {
272278
setContentDescription(console.getName());
273279
}
274280
}
@@ -349,7 +355,7 @@ public void handleException(Throwable exception) {
349355
fPartToConsole.remove(part);
350356
fConsoleToPart.remove(console);
351357
if (fPartToConsole.isEmpty()) {
352-
fActiveConsole = null;
358+
setConsole(null);
353359
}
354360

355361
// update console actions
@@ -377,7 +383,7 @@ protected PageRec doCreatePage(IWorkbenchPart dummyPart) {
377383
console.addPropertyChangeListener(this);
378384

379385
// initialize page participants
380-
IConsolePageParticipant[] consoleParticipants = ((ConsoleManager)getConsoleManager()).getPageParticipants(console);
386+
IConsolePageParticipant[] consoleParticipants = getConsoleManager().getPageParticipants(console);
381387
final ListenerList<IConsolePageParticipant> participants = new ListenerList<>();
382388
for (IConsolePageParticipant consoleParticipant : consoleParticipants) {
383389
participants.add(consoleParticipant);
@@ -414,7 +420,7 @@ public void dispose() {
414420
site.getPage().removePartListener((IPartListener2)this);
415421
}
416422
super.dispose();
417-
ConsoleManager consoleManager = (ConsoleManager) ConsolePlugin.getDefault().getConsoleManager();
423+
ConsoleManager consoleManager = getConsoleManager();
418424
consoleManager.removeConsoleListener(this);
419425
consoleManager.unregisterConsoleView(this);
420426
if (fDisplayConsoleAction != null) {
@@ -429,16 +435,18 @@ public void dispose() {
429435
localResManager.dispose();
430436
localResManager = null;
431437
}
438+
fConsoleToPageParticipants.clear();
439+
fStack.clear();
440+
fConsoleToPart.clear();
441+
fPartToConsole.clear();
432442
ConsolePlugin.getDefault().getPreferenceStore().removePropertyChangeListener(this);
433443
}
434444

435445
/**
436-
* Returns the console manager.
437-
*
438446
* @return the console manager
439447
*/
440-
private IConsoleManager getConsoleManager() {
441-
return ConsolePlugin.getDefault().getConsoleManager();
448+
private ConsoleManager getConsoleManager() {
449+
return (ConsoleManager) ConsolePlugin.getDefault().getConsoleManager();
442450
}
443451

444452
@Override
@@ -502,18 +510,17 @@ public void consolesRemoved(final IConsole[] consoles) {
502510
*/
503511
public ConsoleView() {
504512
super();
513+
fStack = new ArrayList<>();
505514
fConsoleToPart = new HashMap<>();
506515
fPartToConsole = new HashMap<>();
507516
fConsoleToPageParticipants = new HashMap<>();
508-
509-
ConsoleManager consoleManager = (ConsoleManager) ConsolePlugin.getDefault().getConsoleManager();
510-
consoleManager.registerConsoleView(this);
517+
getConsoleManager().registerConsoleView(this);
511518
}
512519

513520
protected void createActions() {
514521
fPinAction = new PinConsoleAction(this);
515522
fDisplayConsoleAction = new ConsoleDropDownAction(this);
516-
ConsoleFactoryExtension[] extensions = ((ConsoleManager)ConsolePlugin.getDefault().getConsoleManager()).getConsoleFactoryExtensions();
523+
ConsoleFactoryExtension[] extensions = getConsoleManager().getConsoleFactoryExtensions();
517524
if (extensions.length > 0) {
518525
fOpenConsoleAction = new OpenConsoleAction(this);
519526
}
@@ -534,8 +541,7 @@ protected void configureToolBar(IToolBarManager mgr) {
534541
public void mouseDown(MouseEvent e) {
535542
ToolItem ti= tb.getItem(new Point(e.x, e.y));
536543
if (ti != null) {
537-
if (ti.getData() instanceof ActionContributionItem) {
538-
ActionContributionItem actionContributionItem= (ActionContributionItem) ti.getData();
544+
if (ti.getData() instanceof ActionContributionItem actionContributionItem) {
539545
IAction action= actionContributionItem.getAction();
540546
if (action == fOpenConsoleAction) {
541547
Event event= new Event();
@@ -554,10 +560,11 @@ public void mouseDown(MouseEvent e) {
554560

555561
@Override
556562
public void display(IConsole console) {
557-
if (fPinned && fActiveConsole != null) {
563+
IConsole activeConsole = getConsole();
564+
if (fPinned && activeConsole != null) {
558565
return;
559566
}
560-
if (console.equals(fActiveConsole)) {
567+
if (console.equals(activeConsole)) {
561568
return;
562569
}
563570
ConsoleWorkbenchPart part = fConsoleToPart.get(console);
@@ -590,8 +597,8 @@ protected IWorkbenchPart getBootstrapPart() {
590597
}
591598

592599
/**
593-
* Registers the given runnable with the display associated with this view's
594-
* control, if any.
600+
* Runs the given runnable with the display associated with this view's control,
601+
* if any.
595602
*
596603
* @param r the runnable
597604
* @see org.eclipse.swt.widgets.Display#asyncExec(java.lang.Runnable)
@@ -622,12 +629,12 @@ public void asyncExec(Runnable r) {
622629
public void createPartControl(Composite parent) {
623630
super.createPartControl(parent);
624631
createActions();
625-
IToolBarManager tbm= getViewSite().getActionBars().getToolBarManager();
626-
configureToolBar(tbm);
632+
IViewSite viewSite = getViewSite();
633+
configureToolBar(viewSite.getActionBars().getToolBarManager());
627634
updateForExistingConsoles();
628-
getViewSite().getActionBars().updateActionBars();
635+
viewSite.getActionBars().updateActionBars();
629636
PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, IConsoleHelpContextIds.CONSOLE_VIEW);
630-
getViewSite().getPage().addPartListener((IPartListener2)this);
637+
viewSite.getPage().addPartListener((IPartListener2) this);
631638
initPageSwitcher();
632639
localResManager = new LocalResourceManager(JFaceResources.getResources(), parent);
633640
updateConsoleIcon = shouldUpdateConsoleIcon();
@@ -701,24 +708,24 @@ public void warnOfContentChange(IConsole console) {
701708
@SuppressWarnings("unchecked")
702709
@Override
703710
public <T> T getAdapter(Class<T> key) {
704-
Object adpater = super.getAdapter(key);
705-
if (adpater == null) {
711+
Object adapter = super.getAdapter(key);
712+
if (adapter == null) {
706713
IConsole console = getConsole();
707714
if (console != null) {
708715
ListenerList<IConsolePageParticipant> listeners = getParticipants(console);
709716
// an adapter can be asked for before the console participants are created
710717
if (listeners != null) {
711718
for (IConsolePageParticipant iConsolePageParticipant : listeners) {
712719
IConsolePageParticipant participant = iConsolePageParticipant;
713-
adpater = participant.getAdapter(key);
714-
if (adpater != null) {
715-
return (T) adpater;
720+
adapter = participant.getAdapter(key);
721+
if (adapter != null) {
722+
return (T) adapter;
716723
}
717724
}
718725
}
719726
}
720727
}
721-
return (T) adpater;
728+
return (T) adapter;
722729
}
723730

724731
@Override
@@ -728,7 +735,7 @@ public void partActivated(IWorkbenchPartReference partRef) {
728735
IContextService contextService = getSite().getService(IContextService.class);
729736
if(contextService != null) {
730737
fActivatedContext = contextService.activateContext(IConsoleConstants.ID_CONSOLE_VIEW);
731-
activateParticipants(fActiveConsole);
738+
activateParticipants(getConsole());
732739
}
733740
}
734741
}
@@ -748,7 +755,7 @@ public void partDeactivated(IWorkbenchPartReference partRef) {
748755
IContextService contextService = getSite().getService(IContextService.class);
749756
if(contextService != null) {
750757
contextService.deactivateContext(fActivatedContext);
751-
deactivateParticipants(fActiveConsole);
758+
deactivateParticipants(getConsole());
752759
}
753760
}
754761
}
@@ -765,8 +772,8 @@ protected boolean isThisPart(IWorkbenchPartReference partRef) {
765772
if (getViewSite() != null && viewRef.getId().equals(getViewSite().getId())) {
766773
String secId = viewRef.getSecondaryId();
767774
String mySec = null;
768-
if (getSite() instanceof IViewSite) {
769-
mySec = ((IViewSite)getSite()).getSecondaryId();
775+
if (getSite() instanceof IViewSite site) {
776+
mySec = site.getSecondaryId();
770777
}
771778
if (mySec == null) {
772779
return secId == null;
@@ -826,8 +833,8 @@ public void setScrollLock(boolean scrollLock) {
826833
fScrollLock = scrollLock;
827834

828835
IPage page = getCurrentPage();
829-
if (page instanceof IOConsolePage) {
830-
((IOConsolePage)page).setAutoScroll(!scrollLock);
836+
if (page instanceof IOConsolePage consolePage) {
837+
consolePage.setAutoScroll(!scrollLock);
831838
}
832839
}
833840

@@ -841,10 +848,10 @@ public void setWordWrap(boolean wordWrap) {
841848
fWordWrap = wordWrap;
842849

843850
IWorkbenchPart part = getSite().getPart();
844-
if (part instanceof PageBookView) {
845-
Control control = ((PageBookView) part).getCurrentPage().getControl();
846-
if (control instanceof StyledText) {
847-
((StyledText) control).setWordWrap(wordWrap);
851+
if (part instanceof PageBookView pagebookView) {
852+
Control control = pagebookView.getCurrentPage().getControl();
853+
if (control instanceof StyledText styledText) {
854+
styledText.setWordWrap(wordWrap);
848855
}
849856
}
850857
}
@@ -870,18 +877,18 @@ public void pin(IConsole console) {
870877
@Override
871878
public void setAutoScrollLock(boolean scrollLock) {
872879
IPage page = getCurrentPage();
873-
if (page instanceof IOConsolePage) {
874-
((IOConsolePage) page).setAutoScroll(!scrollLock);
880+
if (page instanceof IOConsolePage consolePage) {
881+
consolePage.setAutoScroll(!scrollLock);
875882
}
876883

877884
}
878885

879886
@Override
880887
public boolean getAutoScrollLock() {
881888
IPage page = getCurrentPage();
882-
if (page instanceof IOConsolePage) {
883-
return !((IOConsolePage) page).isAutoScroll();
889+
if (page instanceof IOConsolePage consolePage) {
890+
return !consolePage.isAutoScroll();
884891
}
885892
return fScrollLock;
886893
}
887-
}
894+
}

0 commit comments

Comments
 (0)