Source code: com/webobjects/woextensions/WOSortOrderManyKey.java
1 /*
2 * WOSortOrderManyKey.java
3 * © Copyright 2001 Apple Computer, Inc. All rights reserved.
4 * This a modified version.
5 * Original license: http://www.opensource.apple.com/apsl/
6 */
7
8 package com.webobjects.woextensions;
9
10 import com.webobjects.appserver.*;
11 import com.webobjects.foundation.*;
12 import com.webobjects.eocontrol.*;
13
14 import java.util.Enumeration;
15
16 public class WOSortOrderManyKey extends WOComponent {
17 protected String _currentKey;
18 protected String _selectedKey;
19 // ** Internal Caching
20 protected WODisplayGroup _displayGroup;
21
22 public WOSortOrderManyKey(WOContext aContext) {
23 super(aContext);
24 }
25
26 public boolean isStateless() {
27 return true;
28 }
29
30 public void reset() {
31 _currentKey=null;
32 _selectedKey=null;
33 _displayGroup=null;
34 }
35
36 /////////////
37 // Bindings
38 ////////////
39 public WODisplayGroup displayGroup() {
40 if (null==_displayGroup) {
41 _displayGroup = (WODisplayGroup)_WOJExtensionsUtil.valueForBindingOrNull("displayGroup",this);
42 }
43 return _displayGroup;
44 }
45
46 ///////////
47 // Utility
48 ///////////
49 protected EOSortOrdering _primarySortOrdering() {
50 NSArray anArray = displayGroup().sortOrderings();
51 if ((anArray!=null) && (anArray.count() > 0)) {
52 EOSortOrdering anOrdering = (EOSortOrdering)anArray.objectAtIndex(0);
53 return anOrdering;
54 }
55 return null;
56 }
57
58 public void setSelectedKey(String aNewValue) {
59 _selectedKey = aNewValue;
60 if (_isCurrentKeyPrimary()) {
61 _removeSortOrderingWithKey(selectedKey());
62 }
63 _makePrimarySortOrderingWithSelector(EOSortOrdering.CompareAscending);
64 }
65
66 public String selectedKey() {
67 if (null==_selectedKey) {
68 setSelectedKey(_primarySortOrdering().key());
69 }
70 return _selectedKey;
71 }
72
73 protected boolean _isCurrentKeyPrimary() {
74 EOSortOrdering anOrdering = _primarySortOrdering();
75 if ((anOrdering!=null) && anOrdering.key().equals(selectedKey())) {
76 return true;
77 }
78 return false;
79 }
80
81 protected NSSelector _primaryKeySortOrderingSelector() {
82 EOSortOrdering anOrdering = _primarySortOrdering();
83 NSSelector anOrderingSelector = null;
84 if (anOrdering!=null) anOrderingSelector = anOrdering.selector();
85 return anOrderingSelector;
86 }
87
88 protected void _removeSortOrderingWithKey(String aKey) {
89 int anIndex = 0;
90 EOSortOrdering aSortOrdering = null;
91 WODisplayGroup aDisplayGroup = displayGroup();
92 NSArray sortOrderings = aDisplayGroup.sortOrderings();
93 if (sortOrderings!=null) {
94 NSMutableArray aSortOrderingArray = sortOrderings.mutableClone();
95 Enumeration anEnumerator = aSortOrderingArray.objectEnumerator();
96 while (anEnumerator.hasMoreElements()) {
97 aSortOrdering = (EOSortOrdering) anEnumerator.nextElement();
98 if (aKey.equals(aSortOrdering.key())) {
99 aSortOrderingArray.removeObjectAtIndex(anIndex);
100 break;
101 }
102 anIndex++;
103 }
104 aDisplayGroup.setSortOrderings(aSortOrderingArray);
105 }
106 }
107
108 protected void _makePrimarySortOrderingWithSelector(NSSelector aSelector) {
109 String aKey = selectedKey();
110 WODisplayGroup aDisplayGroup = displayGroup();
111 NSArray sortOrderings = aDisplayGroup.sortOrderings();
112 NSMutableArray aSortOrderingArray;
113 if (sortOrderings!=null) {
114 aSortOrderingArray = new NSMutableArray(sortOrderings);
115 } else {
116 aSortOrderingArray = new NSMutableArray();
117 }
118 EOSortOrdering aNewSortOrdering = EOSortOrdering.sortOrderingWithKey(aKey, aSelector);
119 aSortOrderingArray.insertObjectAtIndex(aNewSortOrdering, 0);
120 if (aSortOrderingArray.count() > 3) {
121 // ** limits sorting to 3 levels
122 aSortOrderingArray.removeLastObject();
123 }
124 aDisplayGroup.setSortOrderings(aSortOrderingArray);
125 }
126
127 /////////////
128 // Actions
129 /////////////
130 public WOComponent sortAscendingClicked() {
131 if (_isCurrentKeyPrimary()) {
132 _removeSortOrderingWithKey(selectedKey());
133 }
134 _makePrimarySortOrderingWithSelector(EOSortOrdering.CompareAscending);
135 displayGroup().updateDisplayedObjects();
136 return null;
137 }
138
139 public WOComponent sortDescendingClicked() {
140 if (_isCurrentKeyPrimary()) {
141 _removeSortOrderingWithKey(selectedKey());
142 }
143 _makePrimarySortOrderingWithSelector(EOSortOrdering.CompareDescending);
144 displayGroup().updateDisplayedObjects();
145 return null;
146 }
147
148 }