LINKS_HUD_UI_MANAGING_LINKS: linksHud-implementation.patch
File linksHud-implementation.patch, 21.3 KB (added by pap, 15 years ago) |
---|
-
modules/org.sophie2.main.func.links/src/main/java/org/sophie2/main/func/links/LinksHudLogic.java
### Eclipse Workspace Patch 1.0 #P sophie
1 1 package org.sophie2.main.func.links; 2 2 3 import javax.swing.SwingUtilities; 4 3 5 import org.sophie2.base.bound.BoundControl; 4 6 import org.sophie2.base.bound.ComboInput; 5 7 import org.sophie2.base.commons.util.ImmList; … … 17 19 import org.sophie2.core.mvc.OperationDef; 18 20 import org.sophie2.core.mvc.events.EventR3; 19 21 import org.sophie2.main.app.commons.links.LinkHolder; 20 import org.sophie2.main.func.links.LinksHud.AddRule;21 22 import org.sophie2.main.func.links.LinksHud.AvailableActions; 22 23 import org.sophie2.main.func.links.LinksHud.AvailableTriggers; 23 24 import org.sophie2.main.func.links.LinksHud.CurrentRule; … … 28 29 * Logic, related to the {@link LinksHud}. It handles events fired from there. 29 30 * 30 31 * @author kyli 32 * @author pap 31 33 */ 32 34 public enum LinksHudLogic implements OperationDef { 33 35 … … 49 51 50 52 LinksHud hud = control.findParentElement(LinksHud.class); 51 53 52 if (input.getSelectedItem() != null && 53 hud != null && 54 input.getSelectedItem() != null) { 55 hud.currentRule().set(input.getSelectedItem()); 54 if (hud != null ) { 55 hud.wantedRule().set(input.getSelectedItem()); 56 56 } 57 57 58 58 return true; … … 78 78 BoundControl.EventIds.INPUT_PARAM_INDEX, ComboInput.class); 79 79 80 80 LinksHud hud = control.findParentElement(LinksHud.class); 81 if (hud != null && hud.currentRule().get() != null && input.getSelectedItem() != null) { 82 81 if (hud != null && input.getSelectedItem() != null) { 83 82 LinkHolder holder = hud.holder().get(); 84 83 84 if (hud.currentRule().get() == LinksHud.EMPTY_RULE) { 85 LinkRule newRule = new LinkRule(null, null); 86 ImmList<LinkRule> rules = holder.getLink().getRules(); 87 final Link newLink = new Link(rules.add(newRule)); 88 89 LogicR3.fire(hud, control, null, event, EventIds.SET_LINK, 90 newLink, "Add link rule"); 91 92 assert holder.getLink().getRules().asList().indexOf(newRule) >= 0 : 93 "The rule could not be added!"; 94 hud.wantedRule().set(newRule); 95 } 96 97 98 85 99 LinkTrigger newTrigger = input.getSelectedItem(); 86 100 87 101 LinkRule newRule = new LinkRule(newTrigger, … … 102 116 LogicR3.fire(hud, control, null, event, EventIds.SET_LINK, 103 117 newLink, "Change link trigger"); 104 118 105 hud. currentRule().set(newRule);119 hud.wantedRule().set(newRule); 106 120 } 107 121 return true; 108 122 } … … 121 135 122 136 @SuppressWarnings("unchecked") 123 137 public boolean handle(EventR3 event) { 124 AvailableActions control = event.getSource(AvailableActions.class);138 final AvailableActions control = event.getSource(AvailableActions.class); 125 139 ComboInput<LinkActionProvider> input = event.getEventParam( 126 140 BoundControl.EventIds.INPUT_PARAM_INDEX, ComboInput.class); 127 141 … … 149 163 LogicR3.fire(hud, control, null, event, EventIds.SET_LINK, 150 164 newLink, "Change link action"); 151 165 152 hud.currentRule().set(newRule); 166 hud.wantedRule().set(newRule); 167 SwingUtilities.invokeLater(new Runnable() { 168 169 public void run() { 170 Boolean value = control.actionUpdateForcer().get(); 171 control.actionUpdateForcer().set(!value); 172 } 173 }); 153 174 } 154 175 return true; 155 176 } … … 188 209 LogicR3.fire(hud, control, null, event, EventIds.SET_LINK, 189 210 newLink, "Remove link rule"); 190 211 191 if (newLink.getRules().size() > 0) { 192 hud.currentRule().set(newLink.getRules().get(0)); 193 } else { 194 hud.currentRule().set(null); 195 } 196 return true; 197 } 198 }, 199 200 /** 201 * A new {@link LinkRule} should be added to the {@link Link}. 202 */ 203 ON_ADD_RULE { 204 205 public void defineFilter(EventFilterBuilder filter) { 206 filter.setSourceClass(AddRule.class); 207 filter.setEventId(LinkActionIds.ADD_RULE); 208 } 209 210 public boolean handle(EventR3 event) { 211 AddRule control = event.getSource(AddRule.class); 212 LinksHud hud = control.findParentElement(LinksHud.class); 213 214 if (hud == null) { 215 throw new RuntimeException("Hud not found!"); 216 } 217 LinkHolder holder = hud.holder().get(); 218 219 LinkRule newRule = new LinkRule(null, null); 220 ImmList<LinkRule> rules = holder.getLink().getRules(); 221 final Link newLink = new Link(rules.add(newRule)); 222 223 LogicR3.fire(hud, control, null, event, EventIds.SET_LINK, 224 newLink, "Add link rule"); 225 226 assert holder.getLink().getRules().asList().indexOf(newRule) >= 0 : 227 "The rule could not be added!"; 228 229 hud.currentRule().set(newRule); 212 hud.wantedRule().set(null); 230 213 return true; 231 214 } 232 215 }, -
modules/org.sophie2.main.func.links/src/main/java/org/sophie2/main/func/links/LinksHud.java
2 2 3 3 import java.awt.Font; 4 4 import java.util.ArrayList; 5 import java.util. Collections;5 import java.util.LinkedList; 6 6 import java.util.List; 7 7 8 8 import javax.swing.JPanel; … … 26 26 import org.sophie2.base.model.book.links.LinkActionProvider; 27 27 import org.sophie2.base.model.book.links.LinkRule; 28 28 import org.sophie2.base.model.book.links.LinkTrigger; 29 import org.sophie2.base.skins.Skin; 29 30 import org.sophie2.base.skins.SkinElementId; 31 import org.sophie2.base.skins.SkinManager; 32 import org.sophie2.base.skins.SkinUtil; 30 33 import org.sophie2.base.visual.BaseSwingVisualElement; 31 34 import org.sophie2.base.visual.skins.ElementSkinPart; 32 35 import org.sophie2.base.visual.skins.RelatedChildren; … … 37 40 import org.sophie2.core.mvc.events.EventR3; 38 41 import org.sophie2.core.prolib.annot.Own; 39 42 import org.sophie2.core.prolib.impl.AutoListProperty; 43 import org.sophie2.core.prolib.impl.AutoProperty; 40 44 import org.sophie2.core.prolib.impl.ResourceProperty; 41 45 import org.sophie2.core.prolib.interfaces.ListProp; 42 46 import org.sophie2.core.prolib.interfaces.Prop; … … 50 54 * look for attached rules and configuration panels. 51 55 * 52 56 * @author kyli 57 * @author pap 53 58 */ 54 59 public abstract class LinksHud extends HudDialog { 55 60 61 /** 62 * The {@link Skin} property id of the title of the {@link #EMPTY_RULE} item. 63 */ 64 public static final String EMPTY_RULE_TITLE_PROP_ID = null; 65 56 66 @SuppressWarnings("unused") 57 67 @SkinPartDef 58 68 private static void defineSkin(ElementSkinPart part) { 59 69 part.add(TOOL_TIP_PROP_ID, "Manage links."); 70 part.add(EMPTY_RULE_TITLE_PROP_ID, "New Rule"); 60 71 } 61 72 62 73 /** … … 76 87 part.add(FONT_ID, new Font("Arial", Font.BOLD, 12)); 77 88 part.add(FONT_COLOR_ID, new ImmColor(0.17578125f, 0.26171875f, 0.6875f)); 78 89 } 79 90 80 91 /** 81 92 * The button that closes the links hud. 82 93 * … … 95 106 } 96 107 97 108 } 98 99 109 110 100 111 /** 101 112 * The "current rule" comboBox. 113 * 114 * @author pap 102 115 */ 103 116 @VisualElementDef(parent = LinksHud.class, sortKey = "aaa-current-rule") 104 117 public static class CurrentRule extends BoundComboBox<LinkRule> { … … 118 131 protected ComboState<LinkRule> computeModelData() { 119 132 LinksHud hud = this.findParentElement(LinksHud.class); 120 133 if (hud != null && hud.holder().get() != null) { 121 return new ComboState<LinkRule>("", 122 hud.holder().get().getLink().getRules().asList(), hud.currentRule().get()); 134 List<LinkRule> rules = new ArrayList<LinkRule>(); 135 rules.add(EMPTY_RULE); 136 rules.addAll(hud.holder().get().getLink().getRules().asList()); 137 LinkRule rule = hud.currentRule().get(); 138 rule = rule == null ? EMPTY_RULE : rule ; 123 139 140 return new ComboState<LinkRule>("", rules, rule); 124 141 } 125 142 // else, return empty DropDownList 126 return new ComboState<LinkRule>("", Collections.<LinkRule> emptyList(), null);143 return ComboState.<LinkRule>empty(); 127 144 } 128 145 129 146 @Override 130 147 protected BoundValidation validate(ComboInput<LinkRule> inputData) { 131 148 return BoundValidation.SUCCESS; 132 149 } 133 150 134 151 } 135 152 136 153 /** … … 139 156 @VisualElementDef(parent = LinksHud.class, sortKey = "ddd-available-triggers") 140 157 public static class AvailableTriggers extends BoundComboBox<LinkTrigger> { 141 158 159 /** 160 * The {@link Skin} property id of the title of the {@link #SELECT_TRIGGER_ITEM}. 161 */ 162 public static final String SELECT_TRIGGER_TITLE_PROP_ID = null; 163 164 /** 165 * A special {@link LinkTrigger} to denote a "New rule" item in the combo. 166 */ 167 public static final LinkTrigger SELECT_TRIGGER_ITEM = new LinkTrigger() { 168 169 public String getDescription() { 170 String elemId = SkinUtil.getElementId(AvailableTriggers.class); 171 return SkinManager.get().currentSkin().get().getValue(elemId, SELECT_TRIGGER_TITLE_PROP_ID, String.class); 172 } 173 174 @Override 175 public String toString() { 176 return getDescription(); 177 } 178 }; 179 142 180 @SuppressWarnings("unused") 143 181 @SkinPartDef 144 182 private static void fillSkinParts(ElementSkinPart part) { … … 148 186 part.add(FIELD_EDITABLE_PROP_ID, Boolean.FALSE); 149 187 part.add(COMBO_SIZE_PROP_ID, new ImmSize(220, 20)); 150 188 part.add(VALIDATION_VISIBLE_ID, false); 189 part.add(SELECT_TRIGGER_TITLE_PROP_ID, "choose trigger"); 151 190 } 152 191 153 192 @Override … … 158 197 if (hud != null && hud.holder().get() != null) { 159 198 160 199 // if the holder is a page, get the rule's set trigger. 161 if (hud.currentRule().get() != null) { 162 trigger = hud.currentRule().get().getTrigger(); 163 } else { 164 trigger = null; 165 } 166 return new ComboState<LinkTrigger>("", 167 hud.holder().get().getTriggers(), trigger); 200 LinkRule currentRule = hud.currentRule().get(); 201 if (currentRule != null && currentRule != EMPTY_RULE) { 202 trigger = currentRule.getTrigger(); 203 } else { 204 trigger = SELECT_TRIGGER_ITEM; 205 } 206 return new ComboState<LinkTrigger>("", 207 hud.holder().get().getTriggers(), trigger); 168 208 } 169 209 // else, return empty DropDownList 170 return new ComboState<LinkTrigger>("", Collections.<LinkTrigger> emptyList(), null);210 return ComboState.<LinkTrigger>empty(); 171 211 } 172 212 173 213 @Override 174 214 protected BoundValidation validate(ComboInput<LinkTrigger> inputData) { 175 215 return BoundValidation.SUCCESS; 176 216 } 177 178 @Override 179 public boolean computeEnabled() { 180 LinksHud hud = findParentElement(LinksHud.class); 181 return hud != null && hud.currentRule().get() != null; 182 } 217 183 218 } 184 219 185 220 /** … … 187 222 */ 188 223 @VisualElementDef(parent = LinksHud.class, sortKey = "eee-available-actions") 189 224 public static class AvailableActions extends BoundComboBox<LinkActionProvider> { 225 226 /** 227 * The {@link Skin} property id of the title of the {@link #SELECT_ACTION_ITEM}. 228 */ 229 public static final String SELECT_ACTION_TITLE_PROP_ID = "select-action-item"; 230 231 /** 232 * A special item which purpose is to prompt the user to select an action for the link. 233 */ 234 public static final LinkActionProvider SELECT_ACTION_ITEM = new LinkActionProvider() { 235 236 public Class<? extends LinkAction> getProvidedClass() { 237 return null; 238 } 239 240 public String getDescription() { 241 String elemId = SkinUtil.getElementId(AvailableActions.class); 242 return SkinManager.get().currentSkin().get().getValue(elemId, SELECT_ACTION_TITLE_PROP_ID, String.class); 243 } 244 245 @Override 246 public String toString() { 247 return getDescription(); 248 } 249 250 public LinkAction create() { 251 return null; 252 } 253 }; 190 254 191 255 @SuppressWarnings("unused") 192 256 @SkinPartDef … … 197 261 part.add(FIELD_EDITABLE_PROP_ID, Boolean.FALSE); 198 262 part.add(COMBO_SIZE_PROP_ID, new ImmSize(220, 20)); 199 263 part.add(VALIDATION_VISIBLE_ID, false); 264 part.add(SELECT_ACTION_TITLE_PROP_ID, "choose action"); 200 265 } 201 266 202 267 private ListProp<LinkActionProvider> actionProviders() { … … 208 273 209 274 @Override 210 275 protected List<? extends LinkActionProvider> computeData() { 276 List<LinkActionProvider> res = new ArrayList<LinkActionProvider>(); 211 277 ProList<SophieExtension<LinkActionProvider>> extensions = 212 278 BaseModelBookModule.get().getLinkActionPoint().extensions().get(); 213 List<LinkActionProvider> res = new ArrayList<LinkActionProvider>();214 279 for (int i = 0; i < extensions.size(); i++) { 215 280 res.add(extensions.get(i).getObject()); 216 281 } … … 221 286 } 222 287 return getBean().makeProp(actionProviders.class); 223 288 } 289 290 /** 291 * A property used to make the {@link AvailableActions} control update itself and 292 * remove the "choose action" item when an action is chosen for the first time. 293 * 294 * @return 295 * Property. 296 */ 297 RwProp<Boolean> actionUpdateForcer() { 298 return getBean().makeValueProp("actionUpdateForcer", Boolean.class, true); 299 } 224 300 225 301 @Override 226 302 protected ComboState<LinkActionProvider> computeModelData() { 227 LinksHud hud = this.findParentElement(LinksHud.class);303 LinksHud hud = findParentElement(LinksHud.class); 228 304 LinkActionProvider selectedProvider = null; 305 List<LinkActionProvider> items = new LinkedList<LinkActionProvider>(actionProviders().get()); 229 306 230 if (hud != null && hud.currentRule().get() != null) { 231 LinkAction action = hud.currentRule().get().getAction(); 232 for (LinkActionProvider prov : actionProviders().get()) { 233 if (action != null && prov.getProvidedClass().equals(action.getClass())) { 234 selectedProvider = prov; 235 break; 307 if (hud != null) { 308 actionUpdateForcer().get(); 309 LinkRule currentRule = hud.currentRule().get(); 310 if (currentRule == null || currentRule == EMPTY_RULE || currentRule.getAction() == null) { 311 items.add(0, SELECT_ACTION_ITEM); 312 selectedProvider = SELECT_ACTION_ITEM; 313 } else { 314 LinkAction action = hud.currentRule().get().getAction(); 315 for (LinkActionProvider prov : actionProviders().get()) { 316 if (action != null && prov.getProvidedClass().equals(action.getClass())) { 317 selectedProvider = prov; 318 break; 319 } 236 320 } 237 }238 321 322 } 239 323 } 240 return new ComboState<LinkActionProvider>("", actionProviders().get(), 241 selectedProvider); 324 return new ComboState<LinkActionProvider>("", items, selectedProvider); 242 325 } 243 326 244 327 @Override 245 328 protected BoundValidation validate(ComboInput<LinkActionProvider> inputData) { 246 329 return BoundValidation.SUCCESS; 247 330 } 248 331 249 332 @Override 250 333 public boolean computeEnabled() { 251 334 LinksHud hud = findParentElement(LinksHud.class); 252 335 return hud != null && 253 hud.currentRule().get() != null && 254 hud.currentRule().get().getTrigger() != null; 255 } 256 } 257 258 /** 259 * The "add rule" button. 260 */ 261 @VisualElementDef(parent = LinksHud.class, sortKey = "bbb-add-rule") 262 public static class AddRule extends LogicR3Button { 263 264 @SuppressWarnings("unused") 265 @SkinPartDef 266 private static void fillSkinParts(ElementSkinPart part) { 267 part.add(TOOL_TIP_PROP_ID, "Add new rule"); 268 part.add(SIZE_PROP_ID, new ImmSize(26, 26)); 269 part.add(ICON_PROP_ID, ImageUtil.loadIcon("plus.png")); 270 } 271 272 @Override 273 protected ImmList<EventR3> computeEvents() { 274 return ImmTreeList.<EventR3>create(new EventR3(this, null, null,null, LinkActionIds.ADD_RULE)); 336 hud.currentRule().get() != null && 337 hud.currentRule().get().getTrigger() != null; 275 338 } 276 277 // public Prop<JButton> swingComponent() {278 // class swingComponent extends ResourceProperty<JButton> {279 //280 // @Override281 // protected JButton create() {282 // JButton res =null;283 // res.setName("emptyBackgroundButton");284 //285 // return res;286 // }287 //288 // @Override289 // protected void destroy(JButton res) {290 // // nothing291 // }292 //293 // @Override294 // protected void setup(JButton res) {295 // res.setMargin(new Insets(0, 0, 0, 0));296 // }297 // }298 // return getBean().makeProp(swingComponent.class);299 // }300 339 } 301 340 302 341 /** … … 312 351 part.add(SIZE_PROP_ID, new ImmSize(26, 26)); 313 352 part.add(ICON_PROP_ID, ImageUtil.loadIcon("minus.png")); 314 353 } 315 354 316 355 @Override 317 356 protected ImmList<EventR3> computeEvents() { 318 357 return ImmTreeList.create(new EventR3(this, null, null,null, LinkActionIds.REMOVE_RULE)); 319 358 } 320 321 // public Prop<JButton> swingComponent() { 322 // class swingComponent extends ResourceProperty<JButton> { 323 // 324 // @Override 325 // protected JButton create() { 326 // JButton res = 327 // new LogicR3Button("", 328 // ); 329 // res.setName("emptyBackgroundButton"); 330 // 331 // return res; 332 // } 333 // 334 // @Override 335 // protected void destroy(JButton res) { 336 // // nothing 337 // } 338 // 339 // @Override 340 // protected void setup(JButton res) { 341 // 342 // res.setToolTipText("Remove currently selected rule"); 343 // res.setMargin(new Insets(0, 0, 0, 0)); 344 // } 345 // } 346 // return getBean().makeProp(swingComponent.class); 347 // } 359 360 @Override 361 protected boolean computeEnabled() { 362 LinksHud hud = findParentElement(LinksHud.class); 363 return hud != null && hud.currentRule().get() != null 364 && hud.currentRule().get() != LinksHud.EMPTY_RULE; 365 } 348 366 } 349 367 350 368 /** … … 352 370 */ 353 371 @VisualElementDef(parent = LinksHud.class, sortKey = "fff-configuration-panel") 354 372 public static class ConfigPanel extends BaseSwingVisualElement { 355 373 356 374 @SkinPartDef 357 375 @SuppressWarnings("unused") 358 376 private static void initSkin(ElementSkinPart part) { … … 380 398 381 399 public Prop<JPanel> swingComponent() { 382 400 class swingComponent extends ResourceProperty<JPanel> { 383 401 384 402 private ActionConfigurationPanel currentPanel = null; 385 403 386 404 @Override … … 455 473 @Override 456 474 protected void setup(Object res) { 457 475 holder().get(); 458 currentRule().set(null);476 wantedRule().set(null); 459 477 } 460 478 } 461 479 return getBean().makeProp(currentRuleSync.class); 462 480 } 463 481 464 482 /** 483 * A special {@link LinkRule} denoting that a new rule should be created when the user selects a trigger. 484 */ 485 public static final LinkRule EMPTY_RULE = new LinkRule(null, null) { 486 487 @Override 488 public String toString() { 489 String elemId = SkinUtil.getElementId(LinksHud.class); 490 return SkinManager.get().currentSkin().get().getValue(elemId, EMPTY_RULE_TITLE_PROP_ID, String.class); 491 } 492 }; 493 494 /** 495 * The rule that the user has explicitly set as the one to be edited. 496 * 497 * @return 498 * Property. 499 */ 500 public RwProp<LinkRule> wantedRule() { 501 return getBean().makeValueProp("wantedRule", LinkRule.class, null); 502 } 503 504 /** 465 505 * The rule which is currently selected in the hud. 466 506 * 467 507 * @return the property 468 508 */ 469 public RwProp<LinkRule> currentRule() { 470 return getBean().makeValueProp("currentRule", LinkRule.class, null); 509 public Prop<LinkRule> currentRule() { 510 class currentRule extends AutoProperty<LinkRule> { 511 512 @Override 513 protected LinkRule compute() { 514 if (wantedRule().get() != null) { 515 return wantedRule().get(); 516 } 517 518 LinkRule selected = null; 519 if (holder().get() != null && holder().get().getLink() != null) { 520 ImmList<LinkRule> rules = holder().get().getLink().getRules(); 521 if (!rules.isEmpty()) { 522 selected = rules.get(rules.size() - 1); 523 } else { 524 selected = EMPTY_RULE; 525 } 526 } 527 return selected; 528 } 529 530 } 531 return getBean().makeProp(currentRule.class); 471 532 } 472 533 473 534 /** … … 477 538 * The {@link LinkHolder} to use. 478 539 */ 479 540 public abstract Prop<? extends LinkHolder> holder(); 480 541 481 542 /** 482 * The Ids of the events, fired by the "add" and "remove" buttons.543 * The Ids of the events, fired by LinksHud elements. 483 544 */ 484 545 public enum LinkActionIds { 485 /** 486 * A bew {@link LinkRule} should be added to the current link. 487 */ 488 @EventParams({}) 489 ADD_RULE, 490 546 491 547 /** 492 548 * The currently selected {@link LinkRule} should be removed from the model. 493 549 */ -
modules/org.sophie2.main.func.links/src/main/java/org/sophie2/main/func/links/LinksUtil.java
45 45 LogicR3.fire(hud, null, null, null, EventIds.SET_LINK, 46 46 newLink, "Change link action settings"); 47 47 48 hud. currentRule().set(newRule);48 hud.wantedRule().set(newRule); 49 49 } 50 50 51 51 /** -
modules/org.sophie2.main.func.links/src/main/java/org/sophie2/main/func/links/ElementLinksHud.java
16 16 */ 17 17 @SkinElementId("main.func.links.element-links-hud") 18 18 @RelatedChildren( { 19 LinksHud.CurrentRule.class, LinksHud.AddRule.class, 20 LinksHud.RemoveRule.class, LinksHud.AvailableTriggers.class, 21 LinksHud.AvailableActions.class, LinksHud.ConfigPanel.class, 22 LinksHud.LinksHudTitleBar.class}) 19 LinksHud.CurrentRule.class, 20 LinksHud.RemoveRule.class, 21 LinksHud.AvailableTriggers.class, 22 LinksHud.AvailableActions.class, 23 LinksHud.ConfigPanel.class, 24 LinksHud.LinksHudTitleBar.class 25 }) 23 26 @VisualElementDef(parent = ElementLinksHaloButton.class, sortKey = "kkk-element-links-hud") 24 27 public class ElementLinksHud extends LinksHud { 25 28