package com.simonepezzano.hshare.dialogs;
import java.io.IOException;
import org.dom4j.Attribute;
import org.dom4j.Element;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import com.simonepezzano.hshare.HFiles;
import com.simonepezzano.hshare.HLog;
import com.simonepezzano.hshare.Statics;
import com.simonepezzano.swtu.buttons.BR;
import com.simonepezzano.swtu.labels.LG;
import com.simonepezzano.swtu.text.TG;
/**
* The attributes editing dialog
* @author Simone Pezzano
*
*/
public class AttributesDialog implements SelectionListener{
private Shell window;
private Text akaText;
private Text commentText;
private Text usersText;
private Button ok,cancel;
private Element el;
public static final int WIDTH=400;
public static final int HEIGHT=300;
public static final String TITLE="Edit properties";
/**
* Default constructor
* @param parent the parent Shell
* @param el the element we want to edit
*/
public AttributesDialog(Shell parent,Element el){
this.el = el;
window = new Shell(parent);
window.setSize(WIDTH, HEIGHT);
window.setText(TITLE);
window.setLayout(new GridLayout());
Composite mainComp = new Composite(window,SWT.NONE);
mainComp.setLayoutData(new GridData(GridData.FILL_BOTH));
GridLayout gl = new GridLayout();
gl.numColumns = 2;
mainComp.setLayout(gl);
LG.alignRightLabel(mainComp,"Path");
LG.fillHorLabel(mainComp, getPath());
LG.alignRightLabel(mainComp,"A.K.A.");
akaText = TG.fillHorText(mainComp);
LG.alignRightLabel(mainComp,"Comment");
commentText = TG.fillBothText(mainComp);
if(Statics.isADir(el)){
LG.alignRightLabel(mainComp,"Allowed users");
usersText = new Text(mainComp,SWT.BORDER);
usersText.setToolTipText("A list of comma separated usernames that are allowed to open this folder. Leave empty for public access");
usersText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
}
Composite btnComp = new Composite(window,SWT.NONE);
btnComp.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
btnComp.setLayout(new RowLayout());
ok = BR.widthButton(btnComp, "OK", 100);
cancel = BR.widthButton(btnComp, "Cancel", 100);
ok.addSelectionListener(this);
cancel.addSelectionListener(this);
fillIfNeeded();
}
/**
* @return the path described by the HShare descriptor
*/
private String getPath(){
if(Statics.isAFile(el))
return el.getStringValue();
return el.attributeValue("path");
}
/**
* Sets the data in the fields, if the HShare descriptor contains them
*/
private void fillIfNeeded(){
Attribute aka = el.attribute("aka");
Attribute comment = el.attribute("comment");
Attribute users = el.attribute("users");
if(aka!=null)
akaText.setText(aka.getStringValue());
if(comment!=null)
commentText.setText(comment.getStringValue());
if(users!=null)
usersText.setText(users.getStringValue());
}
/**
* Opens the window
*/
public void open(){
window.open();
}
/**
* Sets the window location
* @param point the top-left point
*/
public void setLocation(Point point){
window.setLocation(point);
}
public void widgetDefaultSelected(SelectionEvent e) {}
public void widgetSelected(SelectionEvent e) {
if(e.getSource().equals(ok)){
Statics.manageAttribute(el, "aka", akaText.getText());
Statics.manageAttribute(el, "comment", commentText.getText());
if(Statics.isADir(el))
Statics.manageAttribute(el, "users", usersText.getText());
try {
HFiles.getInstance().saveHFiles();
} catch (IOException e1) {
HLog.doclogger.fatal("Unable to save list file",e1);
}
}
window.dispose();
}
public void addDisposeListener(DisposeListener disposeListener){
window.addDisposeListener(disposeListener);
}
}