Revision 1877
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.186/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/configurableactions/DefaultConfigurableActionsMamager.java | ||
---|---|---|
1 |
|
|
2 |
package org.gvsig.configurableactions; |
|
3 |
|
|
4 |
import java.lang.ref.WeakReference; |
|
5 |
import java.util.ArrayList; |
|
6 |
import java.util.Collection; |
|
7 |
import java.util.HashMap; |
|
8 |
import java.util.List; |
|
9 |
import java.util.Map; |
|
10 |
import javax.swing.Action; |
|
11 |
import javax.swing.JComponent; |
|
12 |
import org.apache.commons.lang3.StringUtils; |
|
13 |
|
|
14 |
|
|
15 |
public class DefaultConfigurableActionsMamager implements ConfigurableActionsMamager { |
|
16 |
|
|
17 |
private class PanelActions { |
|
18 |
List<Action> actions; |
|
19 |
WeakReference<ActionsComponent> component; |
|
20 |
private final String panelid; |
|
21 |
|
|
22 |
public PanelActions(String panelid) { |
|
23 |
this.panelid = panelid; |
|
24 |
this.actions = new ArrayList<>(); |
|
25 |
} |
|
26 |
|
|
27 |
public void setComponent(ActionsComponent component) { |
|
28 |
this.component = new WeakReference<>(component); |
|
29 |
} |
|
30 |
|
|
31 |
public ActionsComponent getComponent() { |
|
32 |
if( this.component == null ) { |
|
33 |
return null; |
|
34 |
} |
|
35 |
ActionsComponent component = this.component.get(); |
|
36 |
return component; |
|
37 |
} |
|
38 |
|
|
39 |
public void updateComponent() { |
|
40 |
ActionsComponent component = this.getComponent(); |
|
41 |
if( component == null ) { |
|
42 |
return; |
|
43 |
} |
|
44 |
component.update(actions); |
|
45 |
} |
|
46 |
|
|
47 |
public int size() { |
|
48 |
return this.actions.size(); |
|
49 |
} |
|
50 |
|
|
51 |
public Action get(int i) { |
|
52 |
return this.actions.get(i); |
|
53 |
} |
|
54 |
|
|
55 |
public void set(int i, Action action) { |
|
56 |
this.actions.set(i, action); |
|
57 |
this.updateComponent(); |
|
58 |
} |
|
59 |
|
|
60 |
public void add(Action action) { |
|
61 |
this.actions.add(action); |
|
62 |
this.updateComponent(); |
|
63 |
} |
|
64 |
|
|
65 |
public void remove(int i) { |
|
66 |
this.actions.remove(i); |
|
67 |
this.updateComponent(); |
|
68 |
} |
|
69 |
|
|
70 |
public Collection<Action> getActions() { |
|
71 |
return this.actions; |
|
72 |
} |
|
73 |
|
|
74 |
private void removeAll() { |
|
75 |
this.actions.clear(); |
|
76 |
this.updateComponent(); |
|
77 |
} |
|
78 |
|
|
79 |
} |
|
80 |
|
|
81 |
private Map<String,PanelActions> panelsActions; |
|
82 |
|
|
83 |
|
|
84 |
public DefaultConfigurableActionsMamager() { |
|
85 |
this.panelsActions = new HashMap<>(); |
|
86 |
} |
|
87 |
|
|
88 |
@Override |
|
89 |
public void addConfigurableAction(String panelid, Action action) { |
|
90 |
PanelActions panelActions = this.panelsActions.get(panelid); |
|
91 |
if( panelActions==null ) { |
|
92 |
panelActions = new PanelActions(panelid); |
|
93 |
this.panelsActions.put(panelid, panelActions); |
|
94 |
} |
|
95 |
for( int i=0; i<panelActions.size(); i++ ) { |
|
96 |
Action currentAction = panelActions.get(i); |
|
97 |
if( StringUtils.equalsIgnoreCase( |
|
98 |
(String)currentAction.getValue(Action.ACTION_COMMAND_KEY), |
|
99 |
(String)action.getValue(Action.ACTION_COMMAND_KEY) ) ) { |
|
100 |
panelActions.set(i, action); |
|
101 |
return; |
|
102 |
} |
|
103 |
} |
|
104 |
panelActions.add(action); |
|
105 |
} |
|
106 |
|
|
107 |
@Override |
|
108 |
public void removeConfigurableAction(String panelid, String actionName) { |
|
109 |
PanelActions panelActions = this.panelsActions.get(panelid); |
|
110 |
if( panelActions==null ) { |
|
111 |
return; |
|
112 |
} |
|
113 |
for( int i=0; i<panelActions.size(); i++ ) { |
|
114 |
Action currentAction = panelActions.get(i); |
|
115 |
if( StringUtils.equalsIgnoreCase( |
|
116 |
(String)currentAction.getValue(Action.ACTION_COMMAND_KEY), |
|
117 |
actionName ) ) { |
|
118 |
panelActions.remove(i); |
|
119 |
return; |
|
120 |
} |
|
121 |
} |
|
122 |
} |
|
123 |
|
|
124 |
@Override |
|
125 |
public void removeAllConfigurableActions(String panelid) { |
|
126 |
PanelActions panelActions = this.panelsActions.get(panelid); |
|
127 |
if( panelActions==null ) { |
|
128 |
return; |
|
129 |
} |
|
130 |
panelActions.removeAll(); |
|
131 |
} |
|
132 |
|
|
133 |
@Override |
|
134 |
public Collection<Action> getConfigurableActions(String panelid) { |
|
135 |
PanelActions panelActions = this.panelsActions.get(panelid); |
|
136 |
if( panelActions==null ) { |
|
137 |
panelActions = new PanelActions(panelid); |
|
138 |
this.panelsActions.put(panelid, panelActions); |
|
139 |
} |
|
140 |
return panelActions.getActions(); |
|
141 |
} |
|
142 |
|
|
143 |
@Override |
|
144 |
public JComponent getConfigurableActionsComponent(String panelid, Object source) { |
|
145 |
PanelActions panelActions = this.panelsActions.get(panelid); |
|
146 |
if( panelActions==null ) { |
|
147 |
panelActions = new PanelActions(panelid); |
|
148 |
this.panelsActions.put(panelid, panelActions); |
|
149 |
} |
|
150 |
ActionsComponent component = new ActionsComponent(panelActions.getActions(), source); |
|
151 |
panelActions.setComponent(component); |
|
152 |
return component; |
|
153 |
} |
|
154 |
|
|
155 |
} |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.186/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/configurableactions/ActionsComponent.java | ||
---|---|---|
1 |
|
|
2 |
package org.gvsig.configurableactions; |
|
3 |
|
|
4 |
import java.awt.event.ActionEvent; |
|
5 |
import java.awt.event.MouseAdapter; |
|
6 |
import java.awt.event.MouseEvent; |
|
7 |
import java.net.URL; |
|
8 |
import java.util.Collection; |
|
9 |
import javax.swing.AbstractAction; |
|
10 |
import javax.swing.Action; |
|
11 |
import javax.swing.ImageIcon; |
|
12 |
import javax.swing.JLabel; |
|
13 |
import javax.swing.JMenuItem; |
|
14 |
import javax.swing.JPopupMenu; |
|
15 |
import org.gvsig.tools.ToolsLocator; |
|
16 |
import org.gvsig.tools.i18n.I18nManager; |
|
17 |
|
|
18 |
|
|
19 |
public class ActionsComponent extends JLabel { |
|
20 |
|
|
21 |
private final Object source; |
|
22 |
private Collection<Action> actions; |
|
23 |
|
|
24 |
private class ActionListenerAdapter extends AbstractAction { |
|
25 |
|
|
26 |
private final Action action; |
|
27 |
|
|
28 |
public ActionListenerAdapter(Action action) { |
|
29 |
this.action = action; |
|
30 |
} |
|
31 |
|
|
32 |
@Override |
|
33 |
public void actionPerformed(ActionEvent e) { |
|
34 |
e.setSource(source); |
|
35 |
action.actionPerformed(e); |
|
36 |
} |
|
37 |
|
|
38 |
@Override |
|
39 |
public Object getValue(String key) { |
|
40 |
return action.getValue(key); |
|
41 |
} |
|
42 |
|
|
43 |
@Override |
|
44 |
public void putValue(String key, Object newValue) { |
|
45 |
action.putValue(key, newValue); |
|
46 |
} |
|
47 |
|
|
48 |
@Override |
|
49 |
public boolean isEnabled() { |
|
50 |
return action.isEnabled(); |
|
51 |
} |
|
52 |
|
|
53 |
@Override |
|
54 |
public void setEnabled(boolean newValue) { |
|
55 |
action.setEnabled(newValue); |
|
56 |
} |
|
57 |
} |
|
58 |
|
|
59 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
|
60 |
public ActionsComponent(Collection<Action> actions, Object source) { |
|
61 |
this.source = source; |
|
62 |
this.update(actions); |
|
63 |
this.initComponent(); |
|
64 |
} |
|
65 |
|
|
66 |
private void initComponent() { |
|
67 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
|
68 |
this.setToolTipText(i18n.getTranslation("_Click_to_see_more_action_in_this_panel")); |
|
69 |
URL url = this.getClass().getResource("/org/gvsig/configurableactions/images/configurableactions.png"); |
|
70 |
this.setIcon(new ImageIcon(url)); |
|
71 |
this.setBorder(null); |
|
72 |
this.addMouseListener(new MouseAdapter() { |
|
73 |
@Override |
|
74 |
public void mouseClicked(MouseEvent e) { |
|
75 |
if( e.getClickCount()==1 && e.getButton()==MouseEvent.BUTTON1 ) { |
|
76 |
doShowMenu(); |
|
77 |
} |
|
78 |
} |
|
79 |
}); |
|
80 |
// this.addActionListener(new ActionListener() { |
|
81 |
// @Override |
|
82 |
// public void actionPerformed(ActionEvent e) { |
|
83 |
// doShowMenu(); |
|
84 |
// } |
|
85 |
// }); |
|
86 |
} |
|
87 |
|
|
88 |
public void update(Collection<Action> actions) { |
|
89 |
this.actions = actions; |
|
90 |
if( actions==null || actions.isEmpty() ) { |
|
91 |
this.setVisible(false); |
|
92 |
} else { |
|
93 |
this.setVisible(true); |
|
94 |
} |
|
95 |
} |
|
96 |
|
|
97 |
private void doShowMenu() { |
|
98 |
JPopupMenu menu = new JPopupMenu(); |
|
99 |
for (Action action : actions) { |
|
100 |
JMenuItem item = new JMenuItem(new ActionListenerAdapter(action)); |
|
101 |
menu.add(item); |
|
102 |
} |
|
103 |
menu.show(this, 0,this.getHeight()); |
|
104 |
} |
|
105 |
} |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.186/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/propertypage/impl/DefaultPropertiesPageManager.java | ||
---|---|---|
1 |
package org.gvsig.propertypage.impl; |
|
2 |
|
|
3 |
import java.awt.event.ActionEvent; |
|
4 |
import java.util.ArrayList; |
|
5 |
import java.util.Collection; |
|
6 |
import java.util.Collections; |
|
7 |
import java.util.Comparator; |
|
8 |
import java.util.HashMap; |
|
9 |
import java.util.List; |
|
10 |
import java.util.Map; |
|
11 |
import org.gvsig.propertypage.PropertiesPage; |
|
12 |
import org.gvsig.propertypage.PropertiesPage.SetPageEnabledEvent; |
|
13 |
import org.gvsig.propertypage.PropertiesPageFactory; |
|
14 |
import org.gvsig.propertypage.PropertiesPageManager; |
|
15 |
import org.gvsig.tools.ToolsLocator; |
|
16 |
import org.gvsig.tools.identitymanagement.SimpleIdentity; |
|
17 |
import org.gvsig.tools.identitymanagement.SimpleIdentityManager; |
|
18 |
import org.gvsig.tools.util.Invocable; |
|
19 |
import org.slf4j.Logger; |
|
20 |
import org.slf4j.LoggerFactory; |
|
21 |
|
|
22 |
public class DefaultPropertiesPageManager implements PropertiesPageManager { |
|
23 |
|
|
24 |
private static class DefaultSetPageEnabledEvent extends ActionEvent { |
|
25 |
|
|
26 |
private Invocable filter; |
|
27 |
private boolean enabled; |
|
28 |
|
|
29 |
public DefaultSetPageEnabledEvent(Object source, Invocable filter, boolean enabled ) { |
|
30 |
super(source, SetPageEnabledEvent.ID, SetPageEnabledEvent.ACTION_COMMAND); |
|
31 |
this.filter = filter; |
|
32 |
this.enabled = enabled; |
|
33 |
} |
|
34 |
|
|
35 |
public Invocable getFilter() { |
|
36 |
return filter; |
|
37 |
} |
|
38 |
|
|
39 |
public boolean isEnabled() { |
|
40 |
return enabled; |
|
41 |
} |
|
42 |
|
|
43 |
} |
|
44 |
|
|
45 |
public static final String ACCESS_PAGE_AUTHORIZATION = "propertypage-access"; |
|
46 |
|
|
47 |
private static final Logger logger = LoggerFactory.getLogger(DefaultPropertiesPageManager.class); |
|
48 |
|
|
49 |
private final Map<String, Map<String, PropertiesPageFactory>> groups = new HashMap<>(); |
|
50 |
|
|
51 |
public DefaultPropertiesPageManager() { |
|
52 |
|
|
53 |
} |
|
54 |
|
|
55 |
private String getFactoryName(PropertiesPageFactory factory) { |
|
56 |
String factoryName = factory.getClass().getName(); |
|
57 |
try { |
|
58 |
factoryName = factory.getName(); |
|
59 |
} catch(Throwable ex) { |
|
60 |
|
|
61 |
} |
|
62 |
return factoryName; |
|
63 |
} |
|
64 |
|
|
65 |
@Override |
|
66 |
public void registerFactory(PropertiesPageFactory factory) { |
|
67 |
logger.info("Register PropertisPageFactory " + factory.getClass().getName() + " , groupID=" + factory.getGroupID() + " (" + factory.toString() + ")."); |
|
68 |
Map<String, PropertiesPageFactory> group = this.getFactories(factory.getGroupID()); |
|
69 |
group.put(getFactoryName(factory), factory); |
|
70 |
} |
|
71 |
|
|
72 |
private Map<String, PropertiesPageFactory> getFactories(String groupID) { |
|
73 |
logger.info("get propeties page factories for groupID '" + groupID + "'"); |
|
74 |
Map<String,PropertiesPageFactory> group = this.groups.get(groupID); |
|
75 |
if (group == null) { |
|
76 |
group = new HashMap<>(); |
|
77 |
this.groups.put(groupID, group); |
|
78 |
} |
|
79 |
return group; |
|
80 |
} |
|
81 |
|
|
82 |
@Override |
|
83 |
public List<PropertiesPage> getPages(String groupID, Object container, Object data) { |
|
84 |
logger.info("get propeties page for groupID '" + groupID + "', and object '" + ((data==null)?"null":data.toString()) + "'."); |
|
85 |
List<PropertiesPage> pages = new ArrayList<>(); |
|
86 |
SimpleIdentityManager identityManager = ToolsLocator.getIdentityManager(); |
|
87 |
SimpleIdentity currentUser = identityManager.getCurrentIdentity(); |
|
88 |
if (!currentUser.isAuthorized(ACCESS_PAGE_AUTHORIZATION,null,groupID ) ) { |
|
89 |
return pages; // Empty list, no access to pages authoriced |
|
90 |
} |
|
91 |
|
|
92 |
Collection<PropertiesPageFactory> factories = this.getFactories(groupID).values(); |
|
93 |
String authorizationId = ACCESS_PAGE_AUTHORIZATION+"-"+groupID; |
|
94 |
for (PropertiesPageFactory factory : factories) { |
|
95 |
String factoryName = getFactoryName(factory); |
|
96 |
try { |
|
97 |
if (factory.isVisible(data)) { |
|
98 |
PropertiesPage page = factory.create(container, data); |
|
99 |
if (currentUser.isAuthorized(authorizationId,page, page.getTitle()) ) { |
|
100 |
pages.add(page); |
|
101 |
} |
|
102 |
} |
|
103 |
} catch(Throwable ex) { |
|
104 |
logger.warn("Can't get properties page from factory '"+factoryName+"'.",ex); |
|
105 |
} |
|
106 |
} |
|
107 |
Collections.sort(pages, new Comparator<PropertiesPage>() { |
|
108 |
@Override |
|
109 |
public int compare(PropertiesPage f1, PropertiesPage f2) { |
|
110 |
int n = f2.getPriority() - f1.getPriority(); |
|
111 |
if (n != 0) { |
|
112 |
return n; |
|
113 |
} |
|
114 |
if (f1.getTitle() == null || f2.getTitle() == null) { |
|
115 |
// Mas que nada para que no pete, y aunque el orden |
|
116 |
// no sea correcto muestre algo al usuario. |
|
117 |
return 0; |
|
118 |
} |
|
119 |
return f1.getTitle().compareTo(f2.getTitle()); |
|
120 |
} |
|
121 |
}); |
|
122 |
return pages; |
|
123 |
} |
|
124 |
|
|
125 |
@Override |
|
126 |
public ActionEvent createSetPageEnableEvent(PropertiesPage source, Invocable filter, boolean enabled) { |
|
127 |
return new DefaultSetPageEnabledEvent(source, filter, enabled); |
|
128 |
} |
|
129 |
|
|
130 |
@Override |
|
131 |
public ActionEvent createSetPageEnableEvent(final PropertiesPage source, final Class filterClass, final boolean enabled) { |
|
132 |
return new DefaultSetPageEnabledEvent( |
|
133 |
source, |
|
134 |
new Invocable() { |
|
135 |
@Override |
|
136 |
public Object call(Object... propertiesPages) { |
|
137 |
return filterClass.isInstance(propertiesPages[0]); |
|
138 |
} |
|
139 |
}, |
|
140 |
enabled |
|
141 |
); |
|
142 |
} |
|
143 |
|
|
144 |
} |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.186/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/filedialogchooser/impl/VFSJFileChooserBased.java | ||
---|---|---|
1 |
/* |
|
2 |
* To change this license header, choose License Headers in Project Properties. |
|
3 |
* To change this template file, choose Tools | Templates |
|
4 |
* and open the template in the editor. |
|
5 |
*/ |
|
6 |
package org.gvsig.filedialogchooser.impl; |
|
7 |
|
|
8 |
import com.googlecode.vfsjfilechooser2.VFSJFileChooser; |
|
9 |
import com.googlecode.vfsjfilechooser2.filechooser.AbstractVFSFileFilter; |
|
10 |
import java.awt.Component; |
|
11 |
import java.beans.PropertyChangeListener; |
|
12 |
import java.io.File; |
|
13 |
import javax.swing.JComponent; |
|
14 |
import javax.swing.filechooser.FileFilter; |
|
15 |
import org.apache.commons.vfs2.FileObject; |
|
16 |
import org.apache.commons.vfs2.provider.local.LocalFile; |
|
17 |
import org.gvsig.filedialogchooser.FileDialogChooser; |
|
18 |
import org.gvsig.filedialogchooser.FileDialogChooserManager; |
|
19 |
import org.gvsig.tools.util.ToolsUtilLocator; |
|
20 |
|
|
21 |
|
|
22 |
public class VFSJFileChooserBased implements FileDialogChooser { |
|
23 |
|
|
24 |
private static class FileFilterAdapter extends AbstractVFSFileFilter { |
|
25 |
|
|
26 |
private final FileFilter filter; |
|
27 |
|
|
28 |
FileFilterAdapter(FileFilter filter) { |
|
29 |
this.filter = filter; |
|
30 |
} |
|
31 |
|
|
32 |
@Override |
|
33 |
public boolean accept(FileObject f) { |
|
34 |
if( f instanceof LocalFile ) { |
|
35 |
return this.filter.accept( new File(f.getName().getPath())); |
|
36 |
} |
|
37 |
return false; |
|
38 |
} |
|
39 |
|
|
40 |
@Override |
|
41 |
public String getDescription() { |
|
42 |
return this.filter.getDescription(); |
|
43 |
} |
|
44 |
|
|
45 |
public FileFilter getFilter() { |
|
46 |
return this.filter; |
|
47 |
} |
|
48 |
} |
|
49 |
|
|
50 |
private final VFSJFileChooser jfc; |
|
51 |
private final String fileChooserID; |
|
52 |
|
|
53 |
public VFSJFileChooserBased(String fileChooserID) { |
|
54 |
super(); |
|
55 |
this.jfc = new VFSJFileChooser(); |
|
56 |
this.fileChooserID = fileChooserID; |
|
57 |
} |
|
58 |
|
|
59 |
@Override |
|
60 |
public void setCurrentDirectory(File dir) { |
|
61 |
if( dir == null ) { |
|
62 |
dir = new File(System.getProperty("user.home")); |
|
63 |
} |
|
64 |
this.jfc.setCurrentDirectory(dir); |
|
65 |
} |
|
66 |
|
|
67 |
@Override |
|
68 |
public File getCurrentDirectory() { |
|
69 |
return this.jfc.getCurrentDirectory(); |
|
70 |
} |
|
71 |
|
|
72 |
@Override |
|
73 |
public void setDialogTitle(String dialogTitle) { |
|
74 |
this.jfc.setDialogTitle(dialogTitle); |
|
75 |
} |
|
76 |
|
|
77 |
@Override |
|
78 |
public void setDialogType(int dialogType) { |
|
79 |
switch(dialogType) { |
|
80 |
default: |
|
81 |
case OPEN_DIALOG: |
|
82 |
this.jfc.setDialogType(VFSJFileChooser.DIALOG_TYPE.OPEN); |
|
83 |
break; |
|
84 |
case SAVE_DIALOG: |
|
85 |
this.jfc.setDialogType(VFSJFileChooser.DIALOG_TYPE.SAVE); |
|
86 |
break; |
|
87 |
} |
|
88 |
} |
|
89 |
|
|
90 |
@Override |
|
91 |
public void setDragEnabled(boolean b) { |
|
92 |
this.jfc.setDragEnabled(b); |
|
93 |
} |
|
94 |
|
|
95 |
@Override |
|
96 |
public void setFileFilter(FileFilter filter) { |
|
97 |
if( filter == null ) { |
|
98 |
this.jfc.setFileFilter(null); |
|
99 |
} else { |
|
100 |
this.jfc.setFileFilter(new FileFilterAdapter(filter)); |
|
101 |
} |
|
102 |
} |
|
103 |
|
|
104 |
@Override |
|
105 |
public FileFilter getFileFilter() { |
|
106 |
FileFilterAdapter f = (FileFilterAdapter) this.jfc.getFileFilter(); |
|
107 |
return f.getFilter(); |
|
108 |
} |
|
109 |
|
|
110 |
@Override |
|
111 |
public void setFileSelectionMode(int mode) { |
|
112 |
switch(mode) { |
|
113 |
case FILES_ONLY: |
|
114 |
this.jfc.setFileSelectionMode(VFSJFileChooser.SELECTION_MODE.FILES_ONLY); |
|
115 |
break; |
|
116 |
case DIRECTORIES_ONLY: |
|
117 |
this.jfc.setFileSelectionMode(VFSJFileChooser.SELECTION_MODE.DIRECTORIES_ONLY); |
|
118 |
break; |
|
119 |
default: |
|
120 |
case FILES_AND_DIRECTORIES: |
|
121 |
this.jfc.setFileSelectionMode(VFSJFileChooser.SELECTION_MODE.FILES_AND_DIRECTORIES); |
|
122 |
break; |
|
123 |
} |
|
124 |
} |
|
125 |
|
|
126 |
@Override |
|
127 |
public File getSelectedFile() { |
|
128 |
return this.jfc.getSelectedFile(); |
|
129 |
} |
|
130 |
|
|
131 |
@Override |
|
132 |
public File[] getSelectedFiles() { |
|
133 |
return this.jfc.getSelectedFiles(); |
|
134 |
} |
|
135 |
|
|
136 |
@Override |
|
137 |
public void setFileHidingEnabled(boolean b) { |
|
138 |
this.jfc.setFileHidingEnabled(b); |
|
139 |
} |
|
140 |
|
|
141 |
@Override |
|
142 |
public void setMultiSelectionEnabled(boolean b) { |
|
143 |
this.jfc.setMultiSelectionEnabled(b); |
|
144 |
} |
|
145 |
|
|
146 |
@Override |
|
147 |
public boolean isMultiSelectionEnabled() { |
|
148 |
return this.jfc.isMultiSelectionEnabled(); |
|
149 |
} |
|
150 |
|
|
151 |
@Override |
|
152 |
public int showOpenDialog(Component parent) { |
|
153 |
FileDialogChooserManager manager = ToolsUtilLocator.getFileDialogChooserManager(); |
|
154 |
File f = this.getCurrentDirectory(); |
|
155 |
if( f==null ) { |
|
156 |
f = manager.getLastPath(fileChooserID, null); |
|
157 |
this.jfc.setCurrentDirectory(f); |
|
158 |
} |
|
159 |
VFSJFileChooser.RETURN_TYPE r = this.jfc.showOpenDialog(parent); |
|
160 |
if( r == VFSJFileChooser.RETURN_TYPE.CANCEL ) { |
|
161 |
return CANCEL_OPTION; |
|
162 |
} |
|
163 |
if( r == VFSJFileChooser.RETURN_TYPE.APPROVE ) { |
|
164 |
manager.setLastPath(fileChooserID, this.getCurrentDirectory()); |
|
165 |
return APPROVE_OPTION; |
|
166 |
} |
|
167 |
if( r == VFSJFileChooser.RETURN_TYPE.ERROR ) { |
|
168 |
return ERROR_OPTION; |
|
169 |
} |
|
170 |
return ERROR_OPTION; |
|
171 |
} |
|
172 |
|
|
173 |
@Override |
|
174 |
public int showSaveDialog(Component parent) { |
|
175 |
FileDialogChooserManager manager = ToolsUtilLocator.getFileDialogChooserManager(); |
|
176 |
File f = manager.getLastPath(fileChooserID, this.getCurrentDirectory()); |
|
177 |
this.jfc.setCurrentDirectory(f); |
|
178 |
VFSJFileChooser.RETURN_TYPE r = this.jfc.showSaveDialog(parent); |
|
179 |
if( r == VFSJFileChooser.RETURN_TYPE.CANCEL ) { |
|
180 |
return CANCEL_OPTION; |
|
181 |
} |
|
182 |
if( r == VFSJFileChooser.RETURN_TYPE.APPROVE ) { |
|
183 |
manager.setLastPath(fileChooserID, this.getCurrentDirectory()); |
|
184 |
return APPROVE_OPTION; |
|
185 |
} |
|
186 |
if( r == VFSJFileChooser.RETURN_TYPE.ERROR ) { |
|
187 |
return ERROR_OPTION; |
|
188 |
} |
|
189 |
return ERROR_OPTION; |
|
190 |
} |
|
191 |
|
|
192 |
@Override |
|
193 |
public void setAcceptAllFileFilterUsed(boolean b) { |
|
194 |
this.jfc.setAcceptAllFileFilterUsed(b); |
|
195 |
} |
|
196 |
|
|
197 |
@Override |
|
198 |
public void addChoosableFileFilter(FileFilter next) { |
|
199 |
this.jfc.addChoosableFileFilter(new FileFilterAdapter(next)); |
|
200 |
} |
|
201 |
|
|
202 |
@Override |
|
203 |
public void addPropertyChangeListener(PropertyChangeListener listener) { |
|
204 |
this.jfc.addPropertyChangeListener(listener); |
|
205 |
} |
|
206 |
|
|
207 |
@Override |
|
208 |
public void setAccessory(JComponent newAccessory) { |
|
209 |
this.jfc.setAccessory(newAccessory); |
|
210 |
} |
|
211 |
|
|
212 |
} |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.186/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/filedialogchooser/impl/JFileChooserBased.java | ||
---|---|---|
1 |
/* |
|
2 |
* To change this license header, choose License Headers in Project Properties. |
|
3 |
* To change this template file, choose Tools | Templates |
|
4 |
* and open the template in the editor. |
|
5 |
*/ |
|
6 |
package org.gvsig.filedialogchooser.impl; |
|
7 |
|
|
8 |
import java.awt.Component; |
|
9 |
import java.awt.HeadlessException; |
|
10 |
import java.io.File; |
|
11 |
import javax.swing.JFileChooser; |
|
12 |
import org.gvsig.filedialogchooser.FileDialogChooser; |
|
13 |
import org.gvsig.filedialogchooser.FileDialogChooserManager; |
|
14 |
import org.gvsig.tools.util.ToolsUtilLocator; |
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
public class JFileChooserBased extends JFileChooser implements FileDialogChooser { |
|
20 |
|
|
21 |
private static final long serialVersionUID = 2825200958209418322L; |
|
22 |
private final String fileChooserID; |
|
23 |
|
|
24 |
public JFileChooserBased(String fileChooserID) { |
|
25 |
super(); |
|
26 |
this.fileChooserID = fileChooserID; |
|
27 |
} |
|
28 |
|
|
29 |
@Override |
|
30 |
public int showOpenDialog(Component parent) throws HeadlessException { |
|
31 |
FileDialogChooserManager manager = ToolsUtilLocator.getFileDialogChooserManager(); |
|
32 |
File f = this.getCurrentDirectory(); |
|
33 |
if( f==null ) { |
|
34 |
f = manager.getLastPath(fileChooserID, null); |
|
35 |
super.setCurrentDirectory(f); |
|
36 |
} |
|
37 |
int r = super.showOpenDialog(parent); |
|
38 |
if( r == JFileChooser.APPROVE_OPTION ) { |
|
39 |
manager.setLastPath(fileChooserID, this.getCurrentDirectory()); |
|
40 |
} |
|
41 |
return r; |
|
42 |
} |
|
43 |
|
|
44 |
@Override |
|
45 |
public int showSaveDialog(Component parent) throws HeadlessException { |
|
46 |
FileDialogChooserManager manager = ToolsUtilLocator.getFileDialogChooserManager(); |
|
47 |
File f = manager.getLastPath(fileChooserID, this.getCurrentDirectory()); |
|
48 |
super.setCurrentDirectory(f); int r = super.showSaveDialog(parent); |
|
49 |
if( r == JFileChooser.APPROVE_OPTION ) { |
|
50 |
manager.setLastPath(fileChooserID, this.getCurrentDirectory()); |
|
51 |
} |
|
52 |
return r; |
|
53 |
} |
|
54 |
} |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.186/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/filedialogchooser/impl/DefaultFileDialogChooserManager.java | ||
---|---|---|
1 |
|
|
2 |
package org.gvsig.filedialogchooser.impl; |
|
3 |
|
|
4 |
import java.io.File; |
|
5 |
import java.util.HashMap; |
|
6 |
import java.util.Map; |
|
7 |
import org.gvsig.filedialogchooser.FileDialogChooser; |
|
8 |
import org.gvsig.filedialogchooser.FileDialogChooserManager; |
|
9 |
import org.gvsig.tools.ToolsLocator; |
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
public class DefaultFileDialogChooserManager implements FileDialogChooserManager { |
|
14 |
|
|
15 |
private Map<String,File> lastPaths; |
|
16 |
private boolean useNativeFileChooser = true; |
|
17 |
|
|
18 |
public DefaultFileDialogChooserManager() { |
|
19 |
this.lastPaths = new HashMap<>(); |
|
20 |
} |
|
21 |
|
|
22 |
@Override |
|
23 |
public void setUseNativeFileChooser(boolean b) { |
|
24 |
useNativeFileChooser = b; |
|
25 |
} |
|
26 |
|
|
27 |
@Override |
|
28 |
public boolean useNativeFileChooser() { |
|
29 |
return useNativeFileChooser; |
|
30 |
} |
|
31 |
|
|
32 |
@Override |
|
33 |
public FileDialogChooser create(String fileChooserID) { |
|
34 |
if( useNativeFileChooser ) { |
|
35 |
return new JFileChooserBased(fileChooserID); |
|
36 |
} |
|
37 |
return new VFSJFileChooserBased(fileChooserID); |
|
38 |
} |
|
39 |
|
|
40 |
@Override |
|
41 |
public FileDialogChooser create() { |
|
42 |
String fileChooserID = "DEFAULT_ID"; |
|
43 |
if( useNativeFileChooser ) { |
|
44 |
return new JFileChooserBased(fileChooserID); |
|
45 |
} |
|
46 |
return new VFSJFileChooserBased(fileChooserID); |
|
47 |
} |
|
48 |
|
|
49 |
@Override |
|
50 |
public File getLastPath(String fileChooserID, File defaultDirectory) { |
|
51 |
return ToolsLocator.getFoldersManager().getLastPath(fileChooserID, defaultDirectory); |
|
52 |
} |
|
53 |
|
|
54 |
@Override |
|
55 |
public void setLastPath(String fileChooserID, File path) { |
|
56 |
ToolsLocator.getFoldersManager().setLastPath(fileChooserID, path); |
|
57 |
} |
|
58 |
|
|
59 |
|
|
60 |
} |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.186/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/imageviewer/DefaultImageViewer.java | ||
---|---|---|
1 |
|
|
2 |
package org.gvsig.imageviewer; |
|
3 |
|
|
4 |
import java.awt.BorderLayout; |
|
5 |
import java.awt.Dimension; |
|
6 |
import java.awt.Image; |
|
7 |
import java.io.File; |
|
8 |
import java.net.URL; |
|
9 |
import javax.swing.ImageIcon; |
|
10 |
import javax.swing.JComponent; |
|
11 |
import javax.swing.JLabel; |
|
12 |
import javax.swing.JPanel; |
|
13 |
|
|
14 |
|
|
15 |
@SuppressWarnings("UseSpecificCatch") |
|
16 |
public class DefaultImageViewer extends JPanel implements ImageViewer { |
|
17 |
|
|
18 |
private JLabel label; |
|
19 |
|
|
20 |
public DefaultImageViewer() { |
|
21 |
this.initComponents(); |
|
22 |
} |
|
23 |
|
|
24 |
private void initComponents() { |
|
25 |
this.label = new JLabel(); |
|
26 |
this.setLayout(new BorderLayout()); |
|
27 |
this.add(this.label, BorderLayout.CENTER); |
|
28 |
|
|
29 |
this.setPreferredSize( new Dimension(200,350)); |
|
30 |
} |
|
31 |
|
|
32 |
private void setImage(ImageIcon image) { |
|
33 |
this.label.setIcon(image); |
|
34 |
this.setPreferredSize(new Dimension(image.getIconWidth(), image.getIconHeight())); |
|
35 |
} |
|
36 |
|
|
37 |
@Override |
|
38 |
public void setImage(URL image) { |
|
39 |
ImageIcon img = new ImageIcon(image); |
|
40 |
this.setImage(img); |
|
41 |
} |
|
42 |
|
|
43 |
@Override |
|
44 |
public void setImage(File image) { |
|
45 |
try { |
|
46 |
this.setImage(image.toURI().toURL()); |
|
47 |
} catch(Exception ex) { |
|
48 |
throw new IllegalArgumentException(ex); |
|
49 |
} |
|
50 |
} |
|
51 |
|
|
52 |
@Override |
|
53 |
public void setImage(Image image) { |
|
54 |
ImageIcon img = new ImageIcon(image); |
|
55 |
this.setImage(img); |
|
56 |
} |
|
57 |
|
|
58 |
@Override |
|
59 |
public JComponent asJComponent() { |
|
60 |
return this; |
|
61 |
} |
|
62 |
|
|
63 |
@Override |
|
64 |
public void clean() { |
|
65 |
this.label.setIcon(null); |
|
66 |
} |
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
} |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.186/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/imageviewer/DefaultImageViewerManager.java | ||
---|---|---|
1 |
package org.gvsig.imageviewer; |
|
2 |
|
|
3 |
|
|
4 |
public class DefaultImageViewerManager implements ImageViewerManager { |
|
5 |
|
|
6 |
@Override |
|
7 |
public ImageViewer createImageViewer() { |
|
8 |
return new DefaultImageViewer(); |
|
9 |
} |
|
10 |
|
|
11 |
} |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.186/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/desktopopen/DefaultDesktopOpen.java | ||
---|---|---|
1 |
package org.gvsig.desktopopen; |
|
2 |
|
|
3 |
import java.awt.Desktop; |
|
4 |
import java.io.File; |
|
5 |
import java.io.IOException; |
|
6 |
import java.net.URI; |
|
7 |
import java.util.ArrayList; |
|
8 |
import java.util.List; |
|
9 |
import org.slf4j.Logger; |
|
10 |
import org.slf4j.LoggerFactory; |
|
11 |
|
|
12 |
/* |
|
13 |
* Based on portions of code of MightyPork, http://www.ondrovo.com/ |
|
14 |
* extracted from : |
|
15 |
* http://stackoverflow.com/questions/18004150/desktop-api-is-not-supported-on-the-current-platform |
|
16 |
*/ |
|
17 |
public class DefaultDesktopOpen implements DesktopOpen { |
|
18 |
|
|
19 |
private static Logger logger = LoggerFactory.getLogger(DefaultDesktopOpen.class); |
|
20 |
|
|
21 |
public enum EnumOS { |
|
22 |
|
|
23 |
linux, macos, solaris, unknown, windows; |
|
24 |
|
|
25 |
public boolean isLinux() { |
|
26 |
|
|
27 |
return this == linux || this == solaris; |
|
28 |
} |
|
29 |
|
|
30 |
public boolean isMac() { |
|
31 |
|
|
32 |
return this == macos; |
|
33 |
} |
|
34 |
|
|
35 |
public boolean isWindows() { |
|
36 |
|
|
37 |
return this == windows; |
|
38 |
} |
|
39 |
} |
|
40 |
|
|
41 |
@Override |
|
42 |
public boolean browse(URI uri) { |
|
43 |
|
|
44 |
if ( browseDESKTOP(uri) ) { |
|
45 |
return true; |
|
46 |
} |
|
47 |
|
|
48 |
return openSystemSpecific(uri.toString()); |
|
49 |
} |
|
50 |
|
|
51 |
@Override |
|
52 |
public boolean open(File file) { |
|
53 |
|
|
54 |
if ( openDESKTOP(file) ) { |
|
55 |
return true; |
|
56 |
} |
|
57 |
|
|
58 |
return openSystemSpecific(file.getPath()); |
|
59 |
} |
|
60 |
|
|
61 |
@Override |
|
62 |
public boolean edit(File file) { |
|
63 |
|
|
64 |
if ( editDESKTOP(file) ) { |
|
65 |
return true; |
|
66 |
} |
|
67 |
|
|
68 |
return openSystemSpecific(file.getPath()); |
|
69 |
} |
|
70 |
|
|
71 |
private boolean openSystemSpecific(String what) { |
|
72 |
|
|
73 |
EnumOS os = getOs(); |
|
74 |
|
|
75 |
if (os.isLinux()) { |
|
76 |
// if (runCommand("gnome-open", "%s", what)) { |
|
77 |
// return true; |
|
78 |
// } |
|
79 |
// if (runCommand("kde-open", "%s", what)) { |
|
80 |
// return true; |
|
81 |
// } |
|
82 |
if (runCommand("xdg-open", "%s", what)) { |
|
83 |
return true; |
|
84 |
} |
|
85 |
} |
|
86 |
|
|
87 |
if ( os.isMac() ) { |
|
88 |
if ( runCommand("open", "%s", what) ) { |
|
89 |
return true; |
|
90 |
} |
|
91 |
} |
|
92 |
|
|
93 |
if ( os.isWindows() ) { |
|
94 |
if ( runCommand("explorer", "%s", what) ) { |
|
95 |
return true; |
|
96 |
} |
|
97 |
} |
|
98 |
|
|
99 |
return false; |
|
100 |
} |
|
101 |
|
|
102 |
private boolean browseDESKTOP(URI uri) { |
|
103 |
|
|
104 |
logger.info("Trying to use Desktop.getDesktop().browse() with " + uri.toString()); |
|
105 |
try { |
|
106 |
if ( !Desktop.isDesktopSupported() ) { |
|
107 |
logger.warn("Platform is not supported."); |
|
108 |
return false; |
|
109 |
} |
|
110 |
|
|
111 |
if ( !Desktop.getDesktop().isSupported(Desktop.Action.BROWSE) ) { |
|
112 |
logger.warn("BROWSE is not supported."); |
|
113 |
return false; |
|
114 |
} |
|
115 |
|
|
116 |
Desktop.getDesktop().browse(uri); |
|
117 |
|
|
118 |
return true; |
|
119 |
} catch (Throwable t) { |
|
120 |
if( logger.isDebugEnabled() ) { |
|
121 |
logger.warn("Error using desktop browse.", t); |
|
122 |
} else { |
|
123 |
logger.warn("Error using Desktop.getDesktop().browse(). "+ t.toString()); |
|
124 |
} |
|
125 |
return false; |
|
126 |
} |
|
127 |
} |
|
128 |
|
|
129 |
private boolean openDESKTOP(File file) { |
|
130 |
|
|
131 |
logger.info("Trying to use Desktop.getDesktop().open() with " + file.toString()); |
|
132 |
try { |
|
133 |
if ( !Desktop.isDesktopSupported() ) { |
|
134 |
logger.warn("Platform is not supported."); |
|
135 |
return false; |
|
136 |
} |
|
137 |
|
|
138 |
if ( !Desktop.getDesktop().isSupported(Desktop.Action.OPEN) ) { |
|
139 |
logger.warn("OPEN is not supported."); |
|
140 |
return false; |
|
141 |
} |
|
142 |
|
|
143 |
Desktop.getDesktop().open(file); |
|
144 |
|
|
145 |
return true; |
|
146 |
} catch (Throwable t) { |
|
147 |
if( logger.isDebugEnabled() ) { |
|
148 |
logger.warn("Error using desktop open.", t); |
|
149 |
} else { |
|
150 |
logger.warn("Error using Desktop.getDesktop().open(). "+ t.toString()); |
|
151 |
} |
|
152 |
return false; |
|
153 |
} |
|
154 |
} |
|
155 |
|
|
156 |
private boolean editDESKTOP(File file) { |
|
157 |
|
|
158 |
logger.info("Trying to use Desktop.getDesktop().edit() with " + file); |
|
159 |
try { |
|
160 |
if ( !Desktop.isDesktopSupported() ) { |
|
161 |
logger.warn("Platform is not supported."); |
|
162 |
return false; |
|
163 |
} |
|
164 |
|
|
165 |
if ( !Desktop.getDesktop().isSupported(Desktop.Action.EDIT) ) { |
|
166 |
logger.warn("EDIT is not supported."); |
|
167 |
return false; |
|
168 |
} |
|
169 |
|
|
170 |
Desktop.getDesktop().edit(file); |
|
171 |
|
|
172 |
return true; |
|
173 |
} catch (Throwable t) { |
|
174 |
if( logger.isDebugEnabled() ) { |
|
175 |
logger.warn("Error using desktop edit.", t); |
|
176 |
} else { |
|
177 |
logger.warn("Error using Desktop.getDesktop().edit(). "+ t.toString()); |
|
178 |
} |
|
179 |
return false; |
|
180 |
} |
|
181 |
} |
|
182 |
|
|
183 |
private boolean runCommand(String command, String args, String file) { |
|
184 |
|
|
185 |
logger.info("Trying to exec:\n cmd = " + command + "\n args = " + args + "\n %s = " + file); |
|
186 |
|
|
187 |
String[] parts = prepareCommand(command, args, file); |
|
188 |
|
|
189 |
try { |
|
190 |
Process p = Runtime.getRuntime().exec(parts); |
|
191 |
if ( p == null ) { |
|
192 |
return false; |
|
193 |
} |
|
194 |
|
|
195 |
try { |
|
196 |
int retval = p.exitValue(); |
|
197 |
if ( retval == 0 ) { |
|
198 |
logger.warn("Process ended immediately."); |
|
199 |
return false; |
|
200 |
} else { |
|
201 |
logger.warn("Process crashed."); |
|
202 |
return false; |
|
203 |
} |
|
204 |
} catch (IllegalThreadStateException itse) { |
|
205 |
logger.warn("Process is running."); |
|
206 |
return true; |
|
207 |
} |
|
208 |
} catch (IOException e) { |
|
209 |
if( logger.isDebugEnabled() ) { |
|
210 |
logger.warn("Error running command.", e); |
|
211 |
} else { |
|
212 |
logger.warn("Error running command. "+ e.toString()); |
|
213 |
} |
|
214 |
return false; |
|
215 |
} |
|
216 |
} |
|
217 |
|
|
218 |
private String[] prepareCommand(String command, String args, String file) { |
|
219 |
|
|
220 |
List<String> parts = new ArrayList<>(); |
|
221 |
parts.add(command); |
|
222 |
|
|
223 |
if ( args != null ) { |
|
224 |
for ( String s : args.split(" ") ) { |
|
225 |
s = String.format(s, file); // put in the filename thing |
|
226 |
|
|
227 |
parts.add(s.trim()); |
|
228 |
} |
|
229 |
} |
|
230 |
|
|
231 |
return parts.toArray(new String[parts.size()]); |
|
232 |
} |
|
233 |
|
|
234 |
public EnumOS getOs() { |
|
235 |
|
|
236 |
String s = System.getProperty("os.name").toLowerCase(); |
|
237 |
|
|
238 |
if ( s.contains("win") ) { |
|
239 |
return EnumOS.windows; |
|
240 |
} |
|
241 |
|
|
242 |
if ( s.contains("mac") ) { |
|
243 |
return EnumOS.macos; |
|
244 |
} |
|
245 |
|
|
246 |
if ( s.contains("solaris") ) { |
|
247 |
return EnumOS.solaris; |
|
248 |
} |
|
249 |
|
|
250 |
if ( s.contains("sunos") ) { |
|
251 |
return EnumOS.solaris; |
|
252 |
} |
|
253 |
|
|
254 |
if ( s.contains("linux") ) { |
|
255 |
return EnumOS.linux; |
|
256 |
} |
|
257 |
|
|
258 |
if ( s.contains("unix") ) { |
|
259 |
return EnumOS.linux; |
|
260 |
} else { |
|
261 |
return EnumOS.unknown; |
|
262 |
} |
|
263 |
} |
|
264 |
} |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.186/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/htmlbuilder/impl/DefaultHTMLBuilder.java | ||
---|---|---|
1 |
|
|
2 |
package org.gvsig.htmlbuilder.impl; |
|
3 |
|
|
4 |
import java.awt.Color; |
|
5 |
import java.util.ArrayList; |
|
6 |
import java.util.Date; |
|
7 |
import java.util.LinkedHashMap; |
|
8 |
import java.util.List; |
|
9 |
import java.util.Map; |
|
10 |
import org.apache.commons.lang3.StringEscapeUtils; |
|
11 |
import org.apache.commons.lang3.StringUtils; |
|
12 |
import org.gvsig.htmlbuilder.HTMLBuilder; |
|
13 |
import org.gvsig.htmlbuilder.HTMLBuilder.HTMLColor; |
|
14 |
import org.gvsig.htmlbuilder.HTMLBuilder.HTMLComplexElement; |
|
15 |
import org.gvsig.htmlbuilder.HTMLBuilder.HTMLElement; |
|
16 |
import org.gvsig.htmlbuilder.HTMLBuilder.HTMLElementWithAttributes; |
|
17 |
import org.gvsig.htmlbuilder.HTMLBuilder.HTMLElementWithContents; |
|
18 |
|
|
19 |
/** |
|
20 |
* |
|
21 |
* @author jjdelcerro |
|
22 |
*/ |
|
23 |
public class DefaultHTMLBuilder implements HTMLBuilder { |
|
24 |
|
|
25 |
public class CustomHTMLElement implements HTMLElement { |
|
26 |
|
|
27 |
private final String text; |
|
28 |
|
|
29 |
CustomHTMLElement(String text) { |
|
30 |
this.text = text; |
|
31 |
} |
|
32 |
|
|
33 |
@Override |
|
34 |
public boolean allowContents() { |
|
35 |
return false; |
|
36 |
} |
|
37 |
|
|
38 |
@Override |
|
39 |
public boolean allowAttributes() { |
|
40 |
return false; |
|
41 |
} |
|
42 |
|
|
43 |
@Override |
|
44 |
public String toHTML() { |
|
45 |
return this.text; |
|
46 |
} |
|
47 |
|
|
48 |
@Override |
|
49 |
public String toString() { |
|
50 |
return this.toHTML(); |
|
51 |
} |
|
52 |
|
|
53 |
} |
|
54 |
|
|
55 |
public class ContentsHTMLElement implements HTMLElementWithContents { |
|
56 |
private final List<HTMLElement>contents; |
|
57 |
|
|
58 |
public ContentsHTMLElement() { |
|
59 |
this.contents = new ArrayList<>(); |
|
60 |
} |
|
61 |
|
|
62 |
@Override |
|
63 |
public boolean allowContents() { |
|
64 |
return true; |
|
65 |
} |
|
66 |
|
|
67 |
@Override |
|
68 |
public boolean allowAttributes() { |
|
69 |
return false; |
|
70 |
} |
|
71 |
|
|
72 |
@Override |
|
73 |
public String toHTML() { |
|
74 |
StringBuilder builder = new StringBuilder(); |
|
75 |
for (HTMLElement content : this.contents) { |
|
76 |
builder.append(content.toHTML()); |
|
77 |
} |
|
78 |
return builder.toString(); |
|
79 |
} |
|
80 |
|
|
81 |
@Override |
|
82 |
public String toString() { |
|
83 |
return this.toHTML(); |
|
84 |
} |
|
85 |
|
|
86 |
@Override |
|
87 |
public HTMLElementWithContents contents(Object... values) { |
|
88 |
if( values == null ) { |
|
89 |
return this; |
|
90 |
} |
|
91 |
for (Object value : values) { |
|
92 |
if( value == null ) { |
|
93 |
continue; |
|
94 |
} |
|
95 |
if( value instanceof HTMLElement ) { |
|
96 |
this.contents.add((HTMLElement) value); |
|
97 |
} else { |
|
98 |
this.contents.add(custom(value.toString())); |
|
99 |
} |
|
100 |
} |
|
101 |
return this; |
|
102 |
} |
|
103 |
|
|
104 |
|
|
105 |
@Override |
|
106 |
public List<HTMLElement> getContents() { |
|
107 |
return this.contents; |
|
108 |
} |
|
109 |
|
|
110 |
} |
|
111 |
|
|
112 |
public class DefaultHTMLElement implements HTMLComplexElement { |
|
113 |
|
|
114 |
private final String name; |
|
115 |
private final boolean allowContents; |
|
116 |
private final boolean allowAttributes; |
|
117 |
private Map<String,String> attributes; |
|
118 |
private Map<String,String> style; |
|
119 |
private ContentsHTMLElement contents; |
|
120 |
|
|
121 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
|
122 |
public DefaultHTMLElement(String name, |
|
123 |
boolean allowContents, |
|
124 |
boolean allowAttributes, |
|
125 |
Object... contents |
|
126 |
) { |
|
127 |
this.name = name; |
|
128 |
this.allowAttributes = allowAttributes; |
|
129 |
this.allowContents = allowContents; |
|
130 |
this.attributes = null; |
|
131 |
this.contents = null; |
|
132 |
if( this.allowContents ) { |
|
133 |
this.contents(contents); |
|
134 |
} |
|
135 |
} |
|
136 |
|
|
137 |
@Override |
|
138 |
public Map<String,String> getAttributes() { |
|
139 |
if( this.attributes==null ) { |
|
140 |
this.attributes = new LinkedHashMap<>(); |
|
141 |
} |
|
142 |
return this.attributes; |
|
143 |
} |
|
144 |
|
|
145 |
@Override |
|
146 |
public Map<String,String> getStyle() { |
|
147 |
if( this.style==null ) { |
|
148 |
this.style = new LinkedHashMap<>(); |
|
149 |
} |
|
150 |
return this.style; |
|
151 |
} |
|
152 |
|
|
153 |
@Override |
|
154 |
public List<HTMLElement> getContents() { |
|
155 |
if( this.contents==null ) { |
|
156 |
this.contents = new ContentsHTMLElement(); |
|
157 |
} |
|
158 |
return this.contents.getContents(); |
|
159 |
} |
|
160 |
|
|
161 |
@Override |
|
162 |
public HTMLComplexElement set(String name, String value) { |
|
163 |
if( !this.allowAttributes()) { |
|
164 |
throw new RuntimeException("The element '"+this.name+"' don't allow attributes."); |
|
165 |
} |
|
166 |
this.getAttributes().put(name, value); |
|
167 |
return this; |
|
168 |
} |
|
169 |
|
|
170 |
@Override |
|
171 |
public HTMLComplexElement set(String name) { |
|
172 |
return set(name, (String)null); |
|
173 |
} |
|
174 |
|
|
175 |
@Override |
|
176 |
public HTMLComplexElement set(String name, int value) { |
|
177 |
return set(name, String.valueOf(value)); |
|
178 |
} |
|
179 |
|
|
180 |
@Override |
|
181 |
public HTMLComplexElement set(String name, double value) { |
|
182 |
return set(name, String.valueOf(value)); |
|
183 |
} |
|
184 |
|
|
185 |
@Override |
|
186 |
public HTMLComplexElement set(String name, HTMLColor value) { |
|
187 |
return set(name, value.toHTML()); |
|
188 |
} |
|
189 |
|
|
190 |
@Override |
|
191 |
public HTMLComplexElement style(String name, String value) { |
|
192 |
if( !this.allowAttributes()) { |
|
193 |
throw new RuntimeException("The element '"+this.name+"' don't allow attributes."); |
|
194 |
} |
|
195 |
this.getStyle().put(name, value); |
|
196 |
return this; |
|
197 |
} |
|
198 |
|
|
199 |
@Override |
|
200 |
public HTMLComplexElement style(String name, int value) { |
|
201 |
return style(name, String.valueOf(value)); |
|
202 |
} |
|
203 |
|
|
204 |
@Override |
|
205 |
public HTMLComplexElement style(String name, double value) { |
|
206 |
return style(name, String.valueOf(value)); |
|
207 |
} |
|
208 |
|
|
209 |
@Override |
|
210 |
public HTMLComplexElement style(String name, HTMLColor value) { |
|
211 |
return style(name, value.toHTML()); |
|
212 |
} |
|
213 |
|
|
214 |
@Override |
|
215 |
public HTMLComplexElement contents(Object... values) { |
|
216 |
if( values == null ) { |
|
217 |
return this; |
|
218 |
} |
|
219 |
if( !this.allowContents() ) { |
|
220 |
throw new RuntimeException("The element '"+this.name+"' don't allow contents."); |
|
221 |
} |
|
222 |
for (Object value : values) { |
|
223 |
if( value == null ) { |
|
224 |
continue; |
|
225 |
} |
|
226 |
if( value instanceof HTMLElement ) { |
|
227 |
this.getContents().add((HTMLElement) value); |
|
228 |
} else { |
|
229 |
this.getContents().add(custom(value.toString())); |
|
230 |
} |
|
231 |
} |
|
232 |
return this; |
|
233 |
} |
|
234 |
|
|
235 |
@Override |
|
236 |
public boolean allowContents() { |
|
237 |
return this.allowContents; |
|
238 |
} |
|
239 |
|
|
240 |
@Override |
|
241 |
public boolean allowAttributes() { |
|
242 |
return this.allowAttributes; |
|
243 |
} |
|
244 |
|
|
245 |
@Override |
|
246 |
public String toHTML() { |
|
247 |
StringBuilder builder = new StringBuilder(); |
|
248 |
builder.append("<"); |
|
249 |
builder.append(this.name); |
|
250 |
if( this.attributes!=null ) { |
|
251 |
for (Map.Entry<String, String> entry : this.attributes.entrySet()) { |
|
252 |
builder.append(" "); |
|
253 |
builder.append(entry.getKey()); |
|
254 |
if( entry.getValue()!=null ) { |
|
255 |
builder.append("=\""); |
|
256 |
builder.append(StringEscapeUtils.escapeHtml4(entry.getValue())); |
|
257 |
builder.append("\""); |
|
258 |
} |
|
259 |
} |
|
260 |
} |
|
261 |
if( this.style!=null ) { |
|
262 |
builder.append(" style=\""); |
|
263 |
for (Map.Entry<String, String> entry : this.style.entrySet()) { |
|
264 |
builder.append(entry.getKey()); |
|
265 |
builder.append(":"); |
|
266 |
builder.append(StringEscapeUtils.escapeHtml4(entry.getValue())); |
|
267 |
builder.append(" "); |
|
268 |
} |
|
269 |
builder.append("\""); |
|
270 |
} |
|
271 |
if( this.contents!=null ) { |
|
272 |
builder.append(">\n"); |
|
273 |
builder.append(this.contents.toHTML()); |
|
274 |
builder.append("</"); |
|
275 |
builder.append(this.name); |
|
276 |
builder.append(">\n"); |
|
277 |
} else { |
|
278 |
builder.append("/>\n"); |
|
279 |
} |
|
280 |
return builder.toString(); |
|
281 |
} |
|
282 |
|
|
283 |
@Override |
|
284 |
public String toString() { |
|
285 |
return this.toHTML(); |
|
286 |
} |
|
287 |
|
|
288 |
} |
|
289 |
|
|
290 |
public class DefaultHTMLColor implements HTMLColor { |
|
291 |
|
|
292 |
private String color = "white"; |
|
293 |
|
|
294 |
public DefaultHTMLColor(String color) { |
|
295 |
this.color = color; |
|
296 |
} |
|
297 |
|
|
298 |
public DefaultHTMLColor(int r, int g, int b) { |
|
299 |
this("rgb("+r+","+g+","+b+")"); |
|
300 |
} |
|
301 |
|
|
302 |
public DefaultHTMLColor(Color color) { |
|
303 |
this(color.getRed(), color.getGreen(), color.getBlue()); |
|
304 |
} |
|
305 |
|
|
306 |
@Override |
|
307 |
public boolean allowContents() { |
|
308 |
return false; |
|
309 |
} |
|
310 |
|
|
311 |
@Override |
|
312 |
public boolean allowAttributes() { |
|
313 |
return false; |
|
314 |
} |
|
315 |
|
|
316 |
@Override |
|
317 |
public String toHTML() { |
|
318 |
return this.color; |
|
319 |
} |
|
320 |
|
|
321 |
@Override |
|
322 |
public String toString() { |
|
323 |
return this.color; |
|
324 |
} |
|
325 |
|
|
326 |
|
|
327 |
} |
|
328 |
|
|
329 |
@Override |
|
330 |
public HTMLElement custom(String value) { |
|
331 |
return new CustomHTMLElement(value); |
|
332 |
} |
|
333 |
|
|
334 |
@Override |
|
335 |
public HTMLElement plain(String value) { |
|
336 |
return new CustomHTMLElement(StringEscapeUtils.escapeHtml4(value)); |
|
337 |
} |
|
338 |
|
|
339 |
@Override |
|
340 |
public HTMLElement plainWithNl(String value) { |
|
341 |
return new CustomHTMLElement(StringEscapeUtils.escapeHtml4(value).replace("\n", "<br>\n")); |
|
342 |
} |
|
343 |
|
|
344 |
@Override |
|
345 |
public HTMLElement contents(HTMLElement... values) { |
|
346 |
return new ContentsHTMLElement().contents((Object[]) values); |
|
347 |
} |
|
348 |
|
|
349 |
@Override |
|
350 |
public HTMLColor color(Color color) { |
|
351 |
return new DefaultHTMLColor(color); |
|
352 |
} |
|
353 |
|
|
354 |
@Override |
|
355 |
public HTMLColor color(int r, int g, int b) { |
|
356 |
return new DefaultHTMLColor(r, g, b); |
|
357 |
} |
|
358 |
|
|
359 |
@Override |
|
360 |
public HTMLColor color(String color) { |
|
361 |
return new DefaultHTMLColor(color); |
|
362 |
} |
|
363 |
|
|
364 |
@Override |
|
365 |
public HTMLComplexElement a(String link, Object... contents) { |
|
366 |
return new DefaultHTMLElement("a", true, true, contents); |
|
367 |
} |
|
368 |
|
|
369 |
@Override |
|
370 |
public HTMLComplexElement a(Object... contents) { |
|
371 |
return new DefaultHTMLElement("a", true, true, contents); |
|
372 |
} |
|
373 |
|
|
374 |
@Override |
|
375 |
public HTMLComplexElement abbr(Object... contents) { |
|
376 |
return new DefaultHTMLElement("abbr", true, true, contents); |
Also available in: Unified diff