BrightSide Workbench Full Report + Source Code
PortalContainer.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.zul.portal;
19 
20 import java.io.File;
21 import java.io.IOException;
22 import java.io.OutputStreamWriter;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import org.jdom.Document;
26 import org.jdom.Element;
27 import org.jdom.JDOMException;
28 import org.jdom.input.SAXBuilder;
29 import org.jdom.output.Format;
30 import org.jdom.output.XMLOutputter;
31 import org.turro.elephant.context.Application;
32 import org.turro.elephant.context.ElephantContext;
33 import org.turro.elephant.context.IConstructor;
34 import org.turro.elephant.impl.util.FileUtil;
35 import org.zkoss.zk.ui.event.Event;
36 import org.zkoss.zk.ui.event.EventListener;
37 import org.zkoss.zk.ui.ext.AfterCompose;
38 import org.zkoss.zkmax.ui.event.Events;
39 import org.zkoss.zkmax.zul.Portallayout;
40 
45 public class PortalContainer extends Portallayout implements AfterCompose {
46 
47  private String label;
48  private File userFile, confFile;
49  private PortalColumnSet columns = new PortalColumnSet();
50 
51  public PortalContainer() {
52  setSclass("portalContainer");
53  setVflex("true");
54  addEventListener(Events.ON_PORTAL_MOVE, new EventListener() {
55  @Override
56  public void onEvent(Event event) throws Exception {
57  applyChanges();
58  }
59  });
60  }
61 
62  public void setFiles(File userFile, File confFile) {
63  this.userFile = userFile;
64  this.confFile = confFile;
65  if(userFile.exists()) {
66  this.confFile = userFile;
67  }
68  readConfiguration();
69  }
70 
71  public String getLabel() {
72  return label;
73  }
74 
75  public void applyChanges() {
76  writeConfiguration(Application.getApplication().getConstructor());
77  }
78 
79  private void load(Element root) {
80  label = root.getAttributeValue("label");
81  columns.load(root);
82  for(PortalColumn pc : columns) {
83  appendChild(pc);
84  }
85  }
86 
87  private void readConfiguration() {
88  SAXBuilder builder = new SAXBuilder();
89  try {
90  if(confFile.exists()) {
91  Document doc = builder.build(confFile);
92  load(doc.getRootElement());
93  }
94  } catch (IOException ex) {
95  Logger.getLogger(PortalContainer.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
96  } catch (JDOMException ex) {
97  Logger.getLogger(PortalContainer.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
98  }
99  }
100 
101  private void save(Element root) {
102  root.setAttribute("label", label);
103  columns.save(root, this);
104  }
105 
106  private void writeConfiguration(IConstructor constructor) {
107  Document doc = new Document(new Element("elephant-portal"));
108  save(doc.getRootElement());
109  if(!userFile.exists()) new File(org.amic.util.file.FileUtil.getParentPath(userFile.getAbsolutePath())).mkdirs();
110  try {
111  OutputStreamWriter fw = FileUtil.getFileWriter(userFile);
112  Format fm = Format.getPrettyFormat();
113  fm.setEncoding(ElephantContext.getEncoding());
114  XMLOutputter xo = new XMLOutputter(fm);
115  xo.output(doc, fw);
116  fw.close();
117  } catch (IOException ex) {
118  Logger.getLogger(PortalContainer.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
119  }
120  }
121 
122  @Override
123  public void afterCompose() {
124  for(PortalColumn column : columns) {
125  column.afterCompose();
126  }
127  }
128 
129 }
void save(Element root, PortalContainer container)
void setFiles(File userFile, File confFile)