[Java] naar Clipboard verandering luisteren.

Pagina: 1
Acties:

  • Alarmnummer
  • Registratie: Juli 2001
  • Laatst online: 09-07-2024
Ik wil voor een editor voor de actions cut,copy en paste luisteren naar het clipboard. Maar ik zie dat je bij clipboard niet genotified kan worden als er een verandering is. Ik kan helaas ook niet een nieuw Clipboard object (die wel notified) plaatsen omdat er geen setter is in ToolKit. Wie kan mij helpen?

[edit]JEdit heeft ook geen passende oplossing. Ze gebruiken hun eigen clipboard objecten. Het lijkt me dat het dan niet mogelijk is om een copy of paste buiten het systeem te detecteren.

  • mbravenboer
  • Registratie: Januari 2000
  • Laatst online: 06-11-2025
Als het alleen gaat om eigen operaties op het ClipBoard zou je misschien iets met ClipBoardOwner kunnen doen? Je kunt dan achterhalen wanneer je data wordt overschreven.

ClipBoard aanpassen is uiteraard niet mogelijk omdat de native ClipBoard wordt gebruikt...

Als alle oplossingen niet werken: pollen?

Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment


  • The - DDD
  • Registratie: Januari 2000
  • Laatst online: 03-09 16:40
Op donderdag 15 november 2001 19:48 schreef mbravenboer het volgende:
pollen?
Getverjekkie bah!! Pollen zuigt. Als je het doet, wees verstandig en doe het in een aparte thread met een redelijk lange sleep en eventueel als je app het toelaat een lagere thread prioriteit.

  • mbravenboer
  • Registratie: Januari 2000
  • Laatst online: 06-11-2025
The - DDD: Getverjekkie bah!! Pollen zuigt.
Daarom ook: als alle oplossingen niet werken ;) .
Als je het doet, wees verstandig en doe het in een aparte thread.
Vanzelfsprekend :) .

Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment


  • Alarmnummer
  • Registratie: Juli 2001
  • Laatst online: 09-07-2024
bij netbeans doen ze het ook met een thread..
code:
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/** Clipboard workaround about the bug in the Windows system clipboard.
* This clipboard maintains connection to the system clipboard and
* synchronizes with its content. If a flavour is inserted to the clipboard
* that is not accepted by the system one, this clipboard held a reference
* to it.
*
* @author Jaroslav Tulach, Petr Hamernik
* @version 0.19, Jun 08, 1998
*/
public class CoronaClipboard extends ExClipboard
{
    /** how often to test the change */
    private static final int TIME = 3000;

    /** empty array of flavors */
    private static final DataFlavor[] NONE = new DataFlavor[0];

    /** convertors */
    private ConvertorsCache convertors;
    
    /** second clipboard */
    private Clipboard nextClipboard;
    /** flavors of the old clipboard */
    private DataFlavor[] flavors;

    /** original transferable */
    private Transferable orignalTransferable;

    /**
    * Initializes this clipboard to work with system one
    * @param name name of clipboard
    */
    public CoronaClipboard (String name) {
      this (name, java.awt.Toolkit.getDefaultToolkit ().getSystemClipboard ());
    }

    /**
    * Initializes this clipboard to work with the parameter.
    * @param name name of clipboard
    * @param clipboard the second clipboard
    */
    public CoronaClipboard (String name, Clipboard clipboard) {
      super (name);
      nextClipboard = clipboard;

      // XXX(-trung) getTransferDataFlavors() causes deadlock on JDK 1.3 beta
      // Linux and Solaris.  See refreshClipboard()
      if (Utilities.isUnix())
        return;
      // XXX(-trung)
      
      Timer t = new Timer (TIME, new ActionListener () {
                       public void actionPerformed (ActionEvent ev) {
                         refreshClipboard ();
                       }
                     });
      t.setRepeats (true);
      t.start ();
      refreshClipboard ();
    }

    /** Changes the content of transferable also notifies the previous one,
    * that is lost ownership of clipboard.
    */
    synchronized final void changeTransferable (Transferable t) {
      if (orignalTransferable != null) {
        transferableOwnershipLost (orignalTransferable);
      }
      orignalTransferable = t;
    }

    /** Refresh the contents of the external clipboard with
    * the same content.  This method sets
    * the contents of external clipboard and waits for a change
    * from outside application. As soon as the change occurs the
    * <CODE>lostOwnership()</CODE> call occurs which causes a new
    * call to this method. So all the changes to external clipboard
    * should be catched except the change after setting contents to null.
    * <CODE>refreshLost</CODE> flag handles this situation.
    *
    * @return change event has been fired or false if not
    */
    synchronized boolean refreshClipboard() {
      // XXX(-trung) getTransferDataFlavors() causes deadlock on JDK 1.3 beta
      // Linux and Solaris
      if (Utilities.isUnix())
        return false;
      // XXX(-trung)
      
      Transferable tran = null;

      try {
        tran = nextClipboard.getContents(this);
      } catch (IllegalStateException ise) {
        // Clipboard.getContents() can throw IllegalStateException since JDK 1.3.1
        return false;
      } catch (Throwable ex) {
        // System clipboard is buggy in many JVM implementations, we'd
        // rather be defensive
        return false;
      }
      
      try {
        DataFlavor[] arr = tran.getTransferDataFlavors ();

        if (arr == null) {
            arr = NONE;
        }

        if (!java.util.Arrays.equals (arr, flavors)) {
            // changed flavors
            flavors = arr;
            changeTransferable (null);
            fireClipboardChange ();
            return true;
        }
      } catch (NullPointerException e) {
      }

      return false;
    }

    /** Sets the content to this clipboard and also to the second one.
    * Registers to receive notification about lost of ownership and reacts
    * to such action by clearing content of this clipboard.
    */
    public synchronized void setContents (
      Transferable contents, ClipboardOwner owner
    ) {
      changeTransferable (contents);

      nextClipboard.setContents (convert (contents), owner);
      if (!refreshClipboard ()) {
        fireClipboardChange ();
      }
    }

    /** Getter that delegates to the provided clipboard.
    */
    public synchronized Transferable getContents (Object requestor) {
      Transferable t = nextClipboard.getContents (requestor);
      t = convert (t);
      return t;
    }

    /** Current set of convertors */
    protected Convertor[] getConvertors () {
      return computeConvertors ();
    }
    
    /** Computes all convertors.
    */
    private synchronized Convertor[] computeConvertors () {
      if (convertors == null) {
        convertors = new ConvertorsCache();
      }
      
      return convertors.getConvertors();
    }
    
    private class ConvertorsCache implements LookupListener {
      private Convertor[] convs;
      private Lookup.Result result;
      
      public ConvertorsCache() {
        result = Lookup.getDefault().lookup(
            new Lookup.Template(ExClipboard.Convertor.class)
        );
        result.addLookupListener(this);
        resultChanged(null);
      }
      
      /** A change in lookup occured.
       * @param ev event describing the change
       */
      public void resultChanged(LookupEvent ev) {
        Collection c = result.allInstances();
        Convertor[] temp = new Convertor[c.size()];
        convs = (Convertor[]) c.toArray(temp);
      }
      
      public Convertor[] getConvertors() {
        return convs;
      }
    }
}

  • mbravenboer
  • Registratie: Januari 2000
  • Laatst online: 06-11-2025
Het begint hier steeds meer op een codebase te lijken ;) .

Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment


  • Alarmnummer
  • Registratie: Juli 2001
  • Laatst online: 09-07-2024
Dan kun je het zelf zien.. lijkt me wel zo makkelijk :) Maar ik vind het jammer dat ze dat bij sun niet meteen goed hebben gedaan.

  • mbravenboer
  • Registratie: Januari 2000
  • Laatst online: 06-11-2025
Alarmnummer: Dan kun je het zelf zien.. lijkt me wel zo makkelijk :)
Uiteraard, het was ook allerminst commentaar :) .
Maar ik vind het jammer dat ze dat bij sun niet meteen goed hebben gedaan.
Tja, misschien is het niet op alle platformen goed mogelijk?

Het zou in ieder geval de enige feature zijn die het mogelijk maakt om direct naar aktiviteiten in het systeem te kijken...

Post een RFE zou ik zeggen. Als hij er al is: stem derop :) .

Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment


  • Alarmnummer
  • Registratie: Juli 2001
  • Laatst online: 09-07-2024
Op donderdag 15 november 2001 20:52 schreef mbravenboer het volgende:

[..]

Uiteraard, het was ook allerminst commentaar :) .
[..]

Tja, misschien is het niet op alle platformen goed mogelijk?

Het zou in ieder geval de enige feature zijn die het mogelijk maakt om direct naar aktiviteiten in het systeem te kijken...

Post een RFE zou ik zeggen. Als hij er al is: stem derop :) .
Dat duurt me veel te lang, ik wil het gisteren al hebben.. Ik denk dat ik de forte manier maar moet doen. Vind alleen jammer dat je voor zoiets simpels een Thread/Timer hebt lopen.
Pagina: 1