Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a31c6c093b76fa9687cd2d7545ae69eb35aa2bb4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*******************************************************************************
 * Copyright (c) 2000, 2014 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     vogella GmbH - Bug 287303 - [patch] Add Word Wrap action to Console View
 *******************************************************************************/
package org.eclipse.ui.internal.console;

import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.console.IConsoleConstants;
import org.eclipse.ui.console.IConsoleView;
import org.eclipse.ui.console.TextConsole;
import org.eclipse.ui.console.TextConsolePage;
import org.eclipse.ui.console.TextConsoleViewer;

/**
 * A page for an IOConsole
 *
 * @since 3.1
 *
 */
public class IOConsolePage extends TextConsolePage {

    private ScrollLockAction fScrollLockAction;
    private WordWrapAction fWordWrapAction;

    private boolean fReadOnly;

    private IPropertyChangeListener fPropertyChangeListener;

	private IConsoleView fView;

    public IOConsolePage(TextConsole console, IConsoleView view) {
        super(console, view);
		fView = view;

        fPropertyChangeListener = new IPropertyChangeListener() {
            @Override
			public void propertyChange(PropertyChangeEvent event) {
                String property = event.getProperty();
                if (property.equals(IConsoleConstants.P_CONSOLE_OUTPUT_COMPLETE)) {
                    setReadOnly();
                }
            }
        };
        console.addPropertyChangeListener(fPropertyChangeListener);
    }

    @Override
	public void createControl(Composite parent) {
        super.createControl(parent);
        if (fReadOnly) {
            IOConsoleViewer viewer = (IOConsoleViewer) getViewer();
            viewer.setReadOnly();
        }
    }

    @Override
	protected TextConsoleViewer createViewer(Composite parent) {
		return new IOConsoleViewer(parent, (TextConsole) getConsole(), fView);
    }

    public void setAutoScroll(boolean scroll) {
        IOConsoleViewer viewer = (IOConsoleViewer) getViewer();
        if (viewer != null) {
            viewer.setAutoScroll(scroll);
            fScrollLockAction.setChecked(!scroll);
        }
    }

    public void setWordWrap(boolean wordwrap) {
        IOConsoleViewer viewer = (IOConsoleViewer) getViewer();
        if (viewer != null) {
            viewer.setWordWrap(wordwrap);
            fWordWrapAction.setChecked(wordwrap);
        }
    }

    /**
     * Informs the viewer that it's text widget should not be editable.
     */
    public void setReadOnly() {
        fReadOnly = true;
        IOConsoleViewer viewer = (IOConsoleViewer) getViewer();
        if (viewer != null) {
            viewer.setReadOnly();
        }
    }

    @Override
	protected void createActions() {
        super.createActions();
        fScrollLockAction = new ScrollLockAction(getConsoleView());
        setAutoScroll(!fScrollLockAction.isChecked());
        fWordWrapAction = new WordWrapAction(getConsoleView());
        setWordWrap(fWordWrapAction.isChecked());
    }

    @Override
	protected void contextMenuAboutToShow(IMenuManager menuManager) {
        super.contextMenuAboutToShow(menuManager);
        menuManager.add(fScrollLockAction);
        menuManager.add(fWordWrapAction);
        IOConsoleViewer viewer = (IOConsoleViewer) getViewer();
        if (!viewer.isReadOnly()) {
            menuManager.remove(ActionFactory.CUT.getId());
            menuManager.remove(ActionFactory.PASTE.getId());
        }
    }

	@Override
	protected void configureToolBar(IToolBarManager mgr) {
        super.configureToolBar(mgr);
        mgr.appendToGroup(IConsoleConstants.OUTPUT_GROUP, fScrollLockAction);
        mgr.appendToGroup(IConsoleConstants.OUTPUT_GROUP, fWordWrapAction);
    }

    @Override
	public void dispose() {
        if (fScrollLockAction != null) {
            fScrollLockAction.dispose();
            fScrollLockAction = null;
        }
        if (fWordWrapAction != null) {
            fWordWrapAction.dispose();
            fWordWrapAction = null;
        }
        fView = null;
        getConsole().removePropertyChangeListener(fPropertyChangeListener);
        super.dispose();
    }
}

Back to the top