BrightSide Workbench Full Report + Source Code
Link.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2015 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.zkoss.text;
20 
21 import java.io.File;
22 import java.util.HashMap;
23 import org.turro.string.Strings;
24 import org.turro.command.Command;
25 import org.turro.command.Context;
26 import org.turro.elephant.context.ElephantContext;
27 import org.turro.i18n.I_;
28 import org.turro.zkoss.dialog.SelectionDialog;
29 import org.zkoss.zk.ui.Executions;
30 import org.zkoss.zk.ui.IdSpace;
31 import org.zkoss.zk.ui.select.Selectors;
32 import org.zkoss.zk.ui.select.annotation.Listen;
33 import org.zkoss.zk.ui.select.annotation.Wire;
34 import org.zkoss.zul.Checkbox;
35 import org.zkoss.zul.Div;
36 import org.zkoss.zul.Listbox;
37 import org.zkoss.zul.Textbox;
38 
43 public class Link extends Div implements IdSpace {
44 
45  private final WikiElement wikiElement;
46  private String fileFolder;
47  private boolean readOnlyRepository = false, noTreeRepository = false;
48 
49  @Wire private Textbox link;
50  @Wire private Textbox content;
51  @Wire private Checkbox newTab;
52  @Wire private Listbox cssClass;
53 
54  public Link(WikiElement wikiElement) {
55  HashMap args = new HashMap();
56  args.put("el_label", I_.compatibilityMap());
57  args.put("i_", I_.api());
58  args.put("i", I_.map());
59  args.put("k", I_.byKeyMap());
60  Executions.createComponents("/WEB-INF/_zul/bs/comps/editor/link.zul", this, args);
61  Selectors.wireComponents(this, this, false);
62  Selectors.wireEventListeners(this, this);
63  this.wikiElement = wikiElement;
64  initiate();
65  }
66 
67  public String getFileFolder() {
68  return fileFolder;
69  }
70 
71  public void setFileFolder(String fileFolder) {
72  this.fileFolder = fileFolder;
73  }
74 
75  public void setReadOnlyRepository(boolean readOnlyRepository) {
76  this.readOnlyRepository = readOnlyRepository;
77  }
78 
79  public void setNoTreeRepository(boolean noTreeRepository) {
80  this.noTreeRepository = noTreeRepository;
81  }
82 
83  public boolean renderResult() {
84  wikiElement.getAttributes().clear();
85  wikiElement.addNoValuedAttribute(link.getValue().replaceAll("\\:", "\\\\\\:"));
86  String sclass = cssClass.getSelectedItem() == null ? "" : cssClass.getSelectedItem().getLabel();
87  wikiElement.setContent(content.getValue());
88  if(!Strings.isBlank(sclass)) {
89  wikiElement.addClass(sclass);
90  }
91  if(newTab.isChecked()) {
92  wikiElement.getAttributes().put("&target", "_blank");
93  }
94  return wikiElement.render();
95  }
96 
97  @Listen("onClick = #findfile")
98  public void onFindFile() {
99  final RepositoryWalker repositories = new RepositoryWalker(null);
100 
101  repositories.selectFolder(fileFolder);
102  repositories.setFilter(RepositoryContent.FILE_FILTER);
103  repositories.setReadOnlyRepository(readOnlyRepository);
104  repositories.setNoTreeRepository(noTreeRepository);
105  SelectionDialog.showComponent(getPage(), I_.get("Repository"), repositories, "90%", "90%", new Command() {
106  @Override
107  public Object execute(Context context) {
108  File result = repositories.getResult();
109  if(result != null) {
110  link.setValue(ElephantContext.getRootWebPath() + ElephantContext.getRelativePath(result.getAbsolutePath()));
111  content.setValue(result.getName());
112  }
113  return null;
114  }
115  });
116  }
117 
118  private void initiate() {
119  String slink = wikiElement.getFistNoneValued();
120  if(!Strings.isBlank(slink)) {
121  link.setValue(slink.replaceAll("\\\\\\:", "\\:"));
122  }
123  content.setDisabled(!wikiElement.isCreating());
124  newTab.setChecked(wikiElement.getAttributes().containsKey("&target"));
125  }
126 
127 }
static I18nByKey byKeyMap()
Definition: I_.java:91
static String get(String msg)
Definition: I_.java:41
static I18nApiWrapper api()
Definition: I_.java:65
static I18nMapWrapper map()
Definition: I_.java:69
static I18nCompatibilityMap compatibilityMap()
Definition: I_.java:117
static void showComponent(Page page, String title, Component component, String width, String height, final Command command)
void setNoTreeRepository(boolean noTreeRepository)
void setReadOnlyRepository(boolean readOnlyRepository)
void addNoValuedAttribute(String value)
HashMap< String, String > getAttributes()
void setContent(String content)