| 20 | |
| 21 | = Code Examples with different Properties Kinds = |
| 22 | == How to make == |
| 23 | * !FinalProperty |
| 24 | {{{ |
| 25 | public Prop<FrameView> frameView() { |
| 26 | return getBean().makeFinalProp("frameView", FrameView.class); |
| 27 | } |
| 28 | }}} |
| 29 | |
| 30 | * !AutoProperty |
| 31 | {{{ |
| 32 | public Prop<JScrollPane> scrollPane() { |
| 33 | class scrollPane extends AutoProperty<JScrollPane> { |
| 34 | @Override |
| 35 | protected JScrollPane compute() { |
| 36 | JScrollPane res = new JScrollPane(list().get()); |
| 37 | |
| 38 | res.setVerticalScrollBarPolicy( |
| 39 | ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); |
| 40 | |
| 41 | return res; |
| 42 | } |
| 43 | } |
| 44 | return getBean().makeReadProp(scrollPane.class); |
| 45 | } |
| 46 | }}} |
| 47 | |
| 48 | |
| 49 | * !ResourceProperty |
| 50 | {{{ |
| 51 | public Prop<JMenu> swingMenu() { |
| 52 | class swingMenu extends ResourceProperty<JMenu> { |
| 53 | |
| 54 | @Override |
| 55 | protected JMenu create() { |
| 56 | return new JMenu(); |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | protected void destroy(JMenu resource) { |
| 61 | // TODO Auto-generated method stub |
| 62 | |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | protected void setup(JMenu res) { |
| 67 | List<MenuMember> data = items().get(); |
| 68 | assert data.size() == 0 || Collections.frequency(data, data.get(0)) == 1 : data; |
| 69 | |
| 70 | res.removeAll(); |
| 71 | for(MenuMember m : data) { |
| 72 | res.add(m.swingMenu().get()); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | return getBean().makeReadProp(swingMenu.class); |
| 77 | } |
| 78 | }}} |
| 79 | |
| 80 | * !ParentProperty |
| 81 | {{{ |
| 82 | public RwProp<Page> parent() { |
| 83 | return getBean().makeParentProp(Page.class, "frames"); |
| 84 | } |
| 85 | }}} |
| 86 | |
| 87 | * !UndoProperty |
| 88 | {{{ |
| 89 | public UndoProperty undoManager() { |
| 90 | return getBean().makeUndoProperty(); |
| 91 | } |
| 92 | }}} |
| 93 | |
| 94 | * !ChildrenProperty |
| 95 | {{{ |
| 96 | public RwListProp<PageView> pageViews() { |
| 97 | class PageViews extends ChildrenProperty<PageView> { |
| 98 | @Override |
| 99 | protected ListBinding<?, PageView> getListBinding() { |
| 100 | return new BaseListBinding<Page, PageView>() { |
| 101 | |
| 102 | @Override |
| 103 | protected PageView translateSourceToTarget(Page source) { |
| 104 | return new PageView(source); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | protected RwListProp<Page> computeSource() { |
| 109 | return model().get().pages(); |
| 110 | } |
| 111 | }; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return getBean().makeListProp(PageViews.class); |
| 116 | } |
| 117 | }}} |
| 118 | |
| 119 | == Bad Code Examples == |
| 120 | |
| 121 | * Resource Property is better choice. |
| 122 | |
| 123 | {{{ |
| 124 | public Prop<JComponent> swingComponent() { |
| 125 | class swingComponent extends AutoProperty<JComponent> { |
| 126 | @Override |
| 127 | protected JComponent compute() { |
| 128 | JComponent res = new JPanel(); |
| 129 | res.setLayout(new BoxLayout(res, BoxLayout.Y_AXIS)); |
| 130 | res.setPreferredSize(new Dimension(100, 100)); |
| 131 | res.add(headComponent().get()); |
| 132 | res.add(scrollPane().get()); |
| 133 | res.setVisible(true); |
| 134 | res.validate(); |
| 135 | |
| 136 | return res; |
| 137 | } |
| 138 | } |
| 139 | return getBean().makeReadProp(swingComponent.class); |
| 140 | } |
| 141 | }}} |
| 142 | |