Lines 16-21
Link Here
|
16 |
import org.eclipse.gef.palette.PaletteContainer; |
16 |
import org.eclipse.gef.palette.PaletteContainer; |
17 |
import org.eclipse.gef.palette.PaletteDrawer; |
17 |
import org.eclipse.gef.palette.PaletteDrawer; |
18 |
import org.eclipse.gef.palette.PaletteEntry; |
18 |
import org.eclipse.gef.palette.PaletteEntry; |
|
|
19 |
import org.eclipse.gef.palette.PaletteRoot; |
19 |
import org.eclipse.gef.ui.palette.customize.DefaultEntryPage; |
20 |
import org.eclipse.gef.ui.palette.customize.DefaultEntryPage; |
20 |
import org.eclipse.gef.ui.palette.customize.DrawerEntryPage; |
21 |
import org.eclipse.gef.ui.palette.customize.DrawerEntryPage; |
21 |
import org.eclipse.gef.ui.palette.customize.EntryPage; |
22 |
import org.eclipse.gef.ui.palette.customize.EntryPage; |
Lines 25-31
Link Here
|
25 |
|
26 |
|
26 |
/** |
27 |
/** |
27 |
* <code>PaletteCustomizer</code> is the <code>PaletteCustomizerDialog</code>'s interface |
28 |
* <code>PaletteCustomizer</code> is the <code>PaletteCustomizerDialog</code>'s interface |
28 |
* to the model. This class is responsible for propogating to the model changes made in |
29 |
* to the model. This class is responsible for propagating to the model changes made in |
29 |
* the dialog. |
30 |
* the dialog. |
30 |
* |
31 |
* |
31 |
* @author Pratik Shah |
32 |
* @author Pratik Shah |
Lines 33-38
Link Here
|
33 |
public abstract class PaletteCustomizer { |
34 |
public abstract class PaletteCustomizer { |
34 |
|
35 |
|
35 |
/** |
36 |
/** |
|
|
37 |
* Fix 211065 |
38 |
* ElementData keeps track of the important info about |
39 |
* about the PaletteElements in order to check whether |
40 |
* any changes to the palette have been made. information |
41 |
* about children isn't stored, since all original elements |
42 |
* are being stored, and can be compared against. |
43 |
*/ |
44 |
class ElementData{ |
45 |
private PaletteEntry entry; |
46 |
private PaletteContainer parent; |
47 |
private int index; |
48 |
|
49 |
private ElementData(PaletteEntry entry) |
50 |
{ |
51 |
this.entry = entry; |
52 |
parent = entry.getParent(); |
53 |
index = parent.getChildren().indexOf(entry); |
54 |
} |
55 |
|
56 |
PaletteEntry getEntry(){return entry;} |
57 |
PaletteContainer getParent(){return parent;} |
58 |
int getIndex(){return index;} |
59 |
} |
60 |
|
61 |
private PaletteRoot root = null; |
62 |
private List elements = null; |
63 |
private boolean saved = false; |
64 |
|
65 |
/** |
36 |
* Return true if this container can accept this entry as a new child. By default, this |
66 |
* Return true if this container can accept this entry as a new child. By default, this |
37 |
* method checks to see first if the container has full permissions, then checks |
67 |
* method checks to see first if the container has full permissions, then checks |
38 |
* to see if this container can accept the type of the entry. |
68 |
* to see if this container can accept the type of the entry. |
Lines 47-53
Link Here
|
47 |
|
77 |
|
48 |
/** |
78 |
/** |
49 |
* Indicates whether the given entry can be deleted from the model or not. Whether or not |
79 |
* Indicates whether the given entry can be deleted from the model or not. Whether or not |
50 |
* an entry can be deleted depends on its permsission |
80 |
* an entry can be deleted depends on its permission |
51 |
* ({@link PaletteEntry#getUserModificationPermission()}). |
81 |
* ({@link PaletteEntry#getUserModificationPermission()}). |
52 |
* <p> |
82 |
* <p> |
53 |
* This method will be invoked by <code>PaletteCustomizerDialog</code> to determine |
83 |
* This method will be invoked by <code>PaletteCustomizerDialog</code> to determine |
Lines 207-212
Link Here
|
207 |
* @see #canDelete(PaletteEntry) |
237 |
* @see #canDelete(PaletteEntry) |
208 |
*/ |
238 |
*/ |
209 |
public void performDelete(PaletteEntry entry) { |
239 |
public void performDelete(PaletteEntry entry) { |
|
|
240 |
backupPalette(entry); |
210 |
entry.getParent().remove(entry); |
241 |
entry.getParent().remove(entry); |
211 |
} |
242 |
} |
212 |
|
243 |
|
Lines 220-225
Link Here
|
220 |
* @see #canMoveDown(PaletteEntry) |
251 |
* @see #canMoveDown(PaletteEntry) |
221 |
*/ |
252 |
*/ |
222 |
public void performMoveDown(PaletteEntry entry) { |
253 |
public void performMoveDown(PaletteEntry entry) { |
|
|
254 |
backupPalette(entry); |
223 |
PaletteContainer parent = entry.getParent(); |
255 |
PaletteContainer parent = entry.getParent(); |
224 |
if (!parent.moveDown(entry)) { |
256 |
if (!parent.moveDown(entry)) { |
225 |
// This is the case of a PaletteEntry that is its parent's last child |
257 |
// This is the case of a PaletteEntry that is its parent's last child |
Lines 259-264
Link Here
|
259 |
* @see #canMoveUp(PaletteEntry) |
291 |
* @see #canMoveUp(PaletteEntry) |
260 |
*/ |
292 |
*/ |
261 |
public void performMoveUp(PaletteEntry entry) { |
293 |
public void performMoveUp(PaletteEntry entry) { |
|
|
294 |
backupPalette(entry); |
262 |
PaletteContainer parent = entry.getParent(); |
295 |
PaletteContainer parent = entry.getParent(); |
263 |
if (!parent.moveUp(entry)) { |
296 |
if (!parent.moveUp(entry)) { |
264 |
//This is the case of a PaletteEntry that is its parent's first child |
297 |
//This is the case of a PaletteEntry that is its parent's first child |
Lines 297-303
Link Here
|
297 |
* <code>PaletteCustomizerDialog</code>. |
330 |
* <code>PaletteCustomizerDialog</code>. |
298 |
* </p> |
331 |
* </p> |
299 |
*/ |
332 |
*/ |
300 |
public abstract void revertToSaved(); |
333 |
public void revertToSaved(){ |
|
|
334 |
if (saved) |
335 |
{ |
336 |
List newElements = root.getChildren(); |
337 |
|
338 |
for (int index=0;index<elements.size();index++) |
339 |
{ |
340 |
PaletteEntry entry = ((ElementData)elements.get(index)).getEntry(); |
341 |
|
342 |
newElements.remove(entry); |
343 |
|
344 |
if(entry instanceof PaletteContainer) |
345 |
newElements.addAll(((PaletteContainer)entry).getChildren()); |
346 |
} |
347 |
|
348 |
while(!newElements.isEmpty()) |
349 |
{ |
350 |
PaletteEntry entry = (PaletteEntry)newElements.get(0); |
351 |
newElements.remove(0); |
352 |
entry.getParent().remove(entry); |
353 |
} |
354 |
|
355 |
ElementData ed; |
356 |
int index=0; |
357 |
while (index<elements.size()) |
358 |
{ |
359 |
ed = (ElementData)elements.get(index); |
360 |
|
361 |
if (changed(ed)) |
362 |
{ |
363 |
for(;index<elements.size();index++) |
364 |
{ |
365 |
ed = (ElementData)elements.get(index); |
366 |
revert(ed); |
367 |
} |
368 |
} |
369 |
index++; |
370 |
} |
371 |
} |
372 |
|
373 |
clearBackup(); |
374 |
} |
301 |
|
375 |
|
302 |
/** |
376 |
/** |
303 |
* Persists the changes made to the model. |
377 |
* Persists the changes made to the model. |
Lines 305-310
Link Here
|
305 |
* Called when "OK" or "Apply" are selected in the <code>PaletteCustomizerDialog</code>. |
379 |
* Called when "OK" or "Apply" are selected in the <code>PaletteCustomizerDialog</code>. |
306 |
* </p> |
380 |
* </p> |
307 |
*/ |
381 |
*/ |
308 |
public abstract void save(); |
382 |
public void save(){ |
|
|
383 |
clearBackup(); |
384 |
} |
385 |
|
386 |
/** |
387 |
* Backs up the palette. This involves recording what elements are on the palette and their location |
388 |
* @param entry An entry in the palette |
389 |
* @since 3.2 |
390 |
*/ |
391 |
protected void backupPalette(PaletteEntry entry){ |
392 |
if (!saved) |
393 |
{ |
394 |
root = findRoot(entry); |
395 |
elements = new ArrayList(); |
396 |
backupChildren(root); |
397 |
} |
398 |
|
399 |
saved = true; |
400 |
} |
401 |
|
402 |
/** |
403 |
* Clears the stored backup and makes a new one |
404 |
* @since 3.2 |
405 |
*/ |
406 |
protected void clearBackup(){ |
407 |
saved = false; |
408 |
|
409 |
if (root!=null) |
410 |
backupPalette(root); |
411 |
} |
412 |
|
413 |
/** |
414 |
* Finds the paletteRoot for a given PaletteEntry |
415 |
* @param entry the provided entry |
416 |
* @return entry's PaletteRoot (or itself, if entry is a PaletteRoot, or null if entry is not connected with any paletteRoot |
417 |
* @since 3.2 |
418 |
*/ |
419 |
protected PaletteRoot findRoot(PaletteEntry entry){ |
420 |
if (entry==null||((!(entry instanceof PaletteRoot))&&entry.getParent()==null)) |
421 |
return null; |
422 |
else |
423 |
return (entry instanceof PaletteRoot? (PaletteRoot)entry:findRoot(entry.getParent())); |
424 |
} |
425 |
|
426 |
/** |
427 |
* Recursively adds all children, grandchildren, etc of a parent to the saved elements |
428 |
* @param parent the parent who's children are being backed up |
429 |
* @since 3.2 |
430 |
*/ |
431 |
protected void backupChildren(PaletteContainer parent) |
432 |
{ |
433 |
List children = parent.getChildren(); |
434 |
|
435 |
for(int i=0;i<children.size();i++) |
436 |
{ |
437 |
ElementData ed = new ElementData((PaletteEntry)children.get(i)); |
438 |
elements.add(ed); |
439 |
|
440 |
if (ed.entry instanceof PaletteContainer) |
441 |
backupChildren((PaletteContainer)ed.entry); |
442 |
} |
443 |
} |
444 |
|
445 |
/** |
446 |
* Tests whether the positioning of an element has changed since the palette was saved |
447 |
* @param ed Data pertaining to the elements original location |
448 |
* @return true if changed |
449 |
* @since 3.2 |
450 |
*/ |
451 |
private boolean changed(ElementData ed){ |
452 |
|
453 |
return !((ed.entry.getParent()!=null)&&ed.parent.equals(ed.entry.getParent())&&ed.index==ed.parent.getChildren().indexOf(ed.entry)); |
454 |
} |
309 |
|
455 |
|
|
|
456 |
/** |
457 |
* moves an element back to its original location |
458 |
* @param ed Data pertaining to the elements original location |
459 |
* @since 3.2 |
460 |
*/ |
461 |
private void revert(ElementData ed){ |
462 |
if (ed.entry.getParent()!=null) |
463 |
performDelete(ed.entry); |
464 |
ed.parent.add(ed.index,ed.entry); |
310 |
} |
465 |
} |
|
|
466 |
|
467 |
|
468 |
} |