4040package edu .rice .cs .drjava .plugins .eclipse .views ;
4141
4242import org .eclipse .jface .action .Action ;
43- import org .eclipse .jface .preference .*;
43+ //import org.eclipse.jface.preference.*;
44+ import org .eclipse .core .runtime .Preferences ;
45+ //import org.eclipse.core.runtime.Preferences.*;
46+ import org .eclipse .jface .util .IPropertyChangeListener ;
47+ import org .eclipse .jface .util .PropertyChangeEvent ;
4448import org .eclipse .jface .resource .JFaceResources ;
45- import org .eclipse .jface .util .*;
49+ // import org.eclipse.jface.util.*;
4650import org .eclipse .jface .dialogs .InputDialog ;
4751import org .eclipse .jface .dialogs .IInputValidator ;
4852import org .eclipse .swt .graphics .Color ;
6266import edu .rice .cs .drjava .model .repl .ConsoleDocument ;
6367import edu .rice .cs .util .text .SWTDocumentAdapter ;
6468import edu .rice .cs .util .text .SWTDocumentAdapter .SWTStyle ;
65- import edu .rice .cs .util .text .DocumentAdapter ;
69+ import edu .rice .cs .util .text .SwingDocument ;
6670import edu .rice .cs .util .StringOps ;
6771
68- import java .util .Vector ;
72+ import org .eclipse .ui .IWorkbenchActionConstants ;
73+ import org .eclipse .swt .dnd .Clipboard ;
74+ import org .eclipse .swt .dnd .TextTransfer ;
75+ import org .eclipse .swt .dnd .Transfer ;
76+ import org .eclipse .swt .events .SelectionAdapter ;
77+ import org .eclipse .swt .events .SelectionEvent ;
78+
6979
80+ import java .util .Vector ;
81+ import java .net .URL ;
7082/**
7183 * This class installs listeners and actions between an InteractionsDocument
7284 * in the model and an InteractionsPane in the view.
@@ -149,7 +161,8 @@ public void run() {
149161 // ---- Preferences ----
150162
151163 /** Listens to changes to preferences. */
152- protected IPropertyChangeListener _preferenceListener ;
164+ protected Preferences .IPropertyChangeListener _preferenceListener ;
165+ protected IPropertyChangeListener _jfacePreferenceListener ;
153166
154167 /** Whether to prompt before resetting Interactions. */
155168 protected boolean _promptToReset ;
@@ -173,14 +186,18 @@ public InteractionsController(EclipseInteractionsModel model,
173186 _enabled = true ;
174187
175188 // Initialize preferences
176- IPreferenceStore store = EclipsePlugin .getDefault ().getPreferenceStore ();
189+ Preferences prefs = EclipsePlugin .getDefault ().getPluginPreferences ();
190+ //IPreferenceStore store = EclipsePlugin.getDefault().getPreferenceStore();
177191 _preferenceListener = new PrefChangeListener ();
178- store .addPropertyChangeListener (_preferenceListener );
179- JFaceResources .getFontRegistry ().addListener (_preferenceListener );
192+ _jfacePreferenceListener = new JFacePrefChangeListener ();
193+ prefs .addPropertyChangeListener (_preferenceListener );
194+ //store.addPropertyChangeListener(_preferenceListener);
195+ JFaceResources .getFontRegistry ().addListener (_jfacePreferenceListener );
196+ _updateJFacePreferences ();
180197 _updatePreferences ();
181198
182199 // Put the caret at the end
183- _view .getTextPane ().setCaretOffset (_doc .getDocLength ());
200+ _view .getTextPane ().setCaretOffset (_doc .getLength ());
184201
185202 _addDocumentStyles ();
186203 _setupModel ();
@@ -197,19 +214,27 @@ public void dispose() {
197214 _colorDarkGreen .dispose ();
198215 _colorDarkBlue .dispose ();
199216 _colorYellow .dispose ();
217+ _clipboard .dispose ();
200218
201219 // Remove preference listener
202- IPreferenceStore store = EclipsePlugin .getDefault ().getPreferenceStore ();
220+ Preferences store = EclipsePlugin .getDefault ().getPluginPreferences ();
221+ //IPreferenceStore store = EclipsePlugin.getDefault().getPreferenceStore();
203222 store .removePropertyChangeListener (_preferenceListener );
204- JFaceResources .getFontRegistry ().removeListener (_preferenceListener );
223+ JFaceResources .getFontRegistry ().removeListener (_jfacePreferenceListener );
205224 }
206225
207226 /**
208227 * Reads user-defined preferences and sets up a PropertyChangeListener
209228 * to react to changes.
210229 */
230+ private void _updateJFacePreferences () {
231+ // Update the font
232+ _view .updateFont ();
233+ }
234+
211235 private void _updatePreferences () {
212- IPreferenceStore store = EclipsePlugin .getDefault ().getPreferenceStore ();
236+ Preferences store = EclipsePlugin .getDefault ().getPluginPreferences ();
237+ //IPreferenceStore store = EclipsePlugin.getDefault().getPreferenceStore();
213238
214239 // Notifications
215240 _promptToReset = store .getBoolean (DrJavaConstants .INTERACTIONS_RESET_PROMPT );
@@ -219,10 +244,7 @@ private void _updatePreferences() {
219244 _model .setPrivateAccessible (store .getBoolean (DrJavaConstants .ALLOW_PRIVATE_ACCESS ));
220245
221246 // History size
222- _doc .getHistory ().setMaxSize (store .getInt (DrJavaConstants .HISTORY_MAX_SIZE ));
223-
224- // Update the font
225- _view .updateFont ();
247+ //_doc.getHistory().setMaxSize(store.getInt(DrJavaConstants.HISTORY_MAX_SIZE));
226248
227249 // Set the new interpreter JVM arguments
228250 String jvmArgs = store .getString (DrJavaConstants .JVM_ARGS );
@@ -256,7 +278,7 @@ public EclipseInteractionsModel getInteractionsModel() {
256278 /**
257279 * Accessor method for the DocumentAdapter.
258280 */
259- public DocumentAdapter getDocumentAdapter () {
281+ public SWTDocumentAdapter getDocumentAdapter () {
260282 return _adapter ;
261283 }
262284
@@ -327,7 +349,7 @@ public void modifyText(ModifyEvent e) {
327349 StyledText pane = _view .getTextPane ();
328350 int caretPos = pane .getCaretOffset ();
329351 int promptPos = _doc .getPromptPos ();
330- int docLength = _doc .getDocLength ();
352+ int docLength = _doc .getLength ();
331353
332354 if (_doc .inProgress ()) {
333355 // Scroll to the end of the document, since output has been
@@ -446,14 +468,33 @@ public void run() {
446468 _view .getTextPane ().addVerifyKeyListener (new KeyUpdateListener ());
447469// _view.getTextPane().setKeyBinding(((int) '\t') | SWT.SHIFT, SWT.NULL);
448470
471+ _clipboard = new Clipboard (_view .getSite ().getShell ().getDisplay ());
472+
449473 // Set up menu
450474 _setupMenu ();
451475 }
452476
477+ Clipboard _clipboard ;
453478 /**
454479 * Adds actions to the toolbar menu.
455480 */
456481 protected void _setupMenu () {
482+ final Action copyAction = new CopyAction (_view .getTextPane (), _clipboard );
483+ copyAction .setEnabled (false );
484+
485+ _view .addAction (IWorkbenchActionConstants .COPY , copyAction );
486+
487+ _view .addAction (IWorkbenchActionConstants .PASTE ,
488+ new PasteAction (_view .getTextPane (), _clipboard ));
489+ _view .addSelectionListener (new SelectionAdapter () {
490+ public void widgetSelected (SelectionEvent e ) {
491+ //System.out.println("About to Show:" +
492+ // _view.getTextPane().getSelectionCount());
493+ copyAction .setEnabled (
494+ (_view .getTextPane ().getSelectionCount () > 0 ));
495+ }
496+ });
497+
457498 Action resetInteractionsAction = new Action () {
458499 public void run () {
459500 String title = "Confirm Reset Interactions" ;
@@ -471,9 +512,9 @@ public void run() {
471512 public void run () {
472513 String title = "Interpreter Classpath" ;
473514 StringBuffer cpBuf = new StringBuffer ();
474- Vector <String > classpathElements = _model .getClasspath ();
515+ Vector <URL > classpathElements = _model .getClasspath ();
475516 for (int i = 0 ; i < classpathElements .size (); i ++) {
476- cpBuf .append (classpathElements .get (i ));
517+ cpBuf .append (classpathElements .get (i ). toString () );
477518 if (i + 1 < classpathElements .size ()) {
478519 cpBuf .append ("\n " );
479520 }
@@ -642,7 +683,7 @@ boolean moveRightAction() {
642683 moveToEnd ();
643684 return false ;
644685 }
645- else if (position >= _doc .getDocLength ()) {
686+ else if (position >= _doc .getLength ()) {
646687 // Wrap around to the start
647688 moveToPrompt ();
648689 return false ;
@@ -658,7 +699,7 @@ void moveToEnd() {
658699 final StyledText pane = _view .getTextPane ();
659700 pane .getDisplay ().syncExec (new Runnable () {
660701 public void run () {
661- pane .setCaretOffset (_doc .getDocLength ());
702+ pane .setCaretOffset (_doc .getLength ());
662703 pane .showSelection ();
663704 }
664705 });
@@ -679,9 +720,66 @@ public void run() {
679720 * Class to listen to preference changes and update the
680721 * controller accordingly.
681722 */
682- class PrefChangeListener implements IPropertyChangeListener {
723+ class JFacePrefChangeListener implements IPropertyChangeListener {
683724 public void propertyChange (PropertyChangeEvent event ) {
725+ _updateJFacePreferences ();
726+ }
727+ }
728+ class PrefChangeListener implements Preferences .IPropertyChangeListener {
729+ public void propertyChange (Preferences .PropertyChangeEvent event ) {
684730 _updatePreferences ();
685731 }
686732 }
733+
734+
735+ public class CopyAction extends Action {
736+ protected StyledText _text ;
737+ protected Clipboard _clipboard ;
738+ public CopyAction (StyledText text , Clipboard cp ) {
739+ _text = text ;
740+ _clipboard = cp ;
741+ setText ("Copy" );
742+ setAccelerator (SWT .CTRL | 'C' );
743+ }
744+
745+
746+ public void run () {
747+ //get selection
748+ //TODO: need to disable Copy if selection is empty
749+ if (_text .getSelectionCount () > 0 ) {
750+ String selection = _text .getSelectionText ();
751+
752+ _clipboard .setContents (
753+ new Object [] { selection },
754+ new Transfer [] {TextTransfer .getInstance () }
755+ );
756+ }
757+ }
758+ }
759+
760+ public class PasteAction extends Action {
761+ protected StyledText _text ;
762+ protected Clipboard _clipboard ;
763+ public PasteAction (StyledText text , Clipboard cp ) {
764+ _text = text ;
765+ _clipboard = cp ;
766+ setText ("Paste" );
767+ setAccelerator (SWT .CTRL | 'V' );
768+ }
769+
770+
771+ public void run () {
772+ //get selection
773+ Object selection = _clipboard .getContents (TextTransfer .getInstance ());
774+ if (selection != null ) {
775+ _text .insert (selection .toString ());
776+ }
777+ //if (selection instanceof String) {
778+ //_text.insert( (String)selection);
779+ //}
780+
781+ }
782+ }
783+
784+
687785}
0 commit comments