BrightSide Workbench Full Report + Source Code
PublishedButton.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.attach.zul.navigator;
20 
21 import org.turro.attach.db.AttachPU;
22 import org.turro.attach.entity.Attachment;
23 import org.turro.elephant.util.Images;
24 import org.turro.i18n.I_;
25 import org.turro.jpa.Dao;
26 import org.zkoss.zk.ui.event.Event;
27 import org.zkoss.zk.ui.event.EventListener;
28 import org.zkoss.zk.ui.event.Events;
29 import org.zkoss.zul.Toolbarbutton;
30 
35 public class PublishedButton extends Toolbarbutton {
36 
37  private final String fileName, path;
38  private boolean publishable;
39  private final Dao dao;
40 
41  public PublishedButton(Attachment attachment) {
42  this.fileName = attachment.getFileName();
43  this.path = attachment.getPath();
44  this.publishable = attachment.isPublishable();
45  dao = new AttachPU();
46  updateImage(publishable);
47  initState();
48  }
49 
50  private void initState() {
51  addEventListener(Events.ON_CLICK, new EventListener<Event>() {
52  @Override
53  public void onEvent(Event event) throws Exception {
54  publishable = !publishable;
55  dao.executeUpdate(
56  "update Attachment set publishable = ? " +
57  "where fileName = ? and path = ?",
58  new Object[] { publishable, fileName, path });
59  updateImage(publishable);
60  }
61  });
62  }
63 
64  private void updateImage(boolean publishable) {
65  if(publishable) {
66  setImage(Images.getImage("published"));
67  setTooltiptext(I_.get("Publishable"));
68  } else {
69  setImage(Images.getImage("not_published"));
70  setTooltiptext(I_.get("Internal"));
71  }
72  }
73 
74 }