Skip to content

Instantly share code, notes, and snippets.

@lenards
Created July 12, 2010 16:29
Show Gist options
  • Save lenards/472680 to your computer and use it in GitHub Desktop.
Save lenards/472680 to your computer and use it in GitHub Desktop.
package org.iplantc.de.client.views;
import org.iplantc.de.client.ClientAction;
import org.iplantc.de.client.I18N;
import org.iplantc.de.client.models.File;
import org.iplantc.de.client.views.dialogs.FileSelectDialog;
import org.iplantc.de.client.views.dialogs.IPlantDialog.DialogOkClickHandler;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.Composite;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
/**
* Models a composite widget for selecting a file from a user's workspace.
*
* @author lenards
*
*/
public class FileSelector extends Composite
{
public static final String CALLER_TAG = "file_select_";
public static final String DEFAULT_WIDTH = "620px";
// TODO: i18n this caption...
public static final String DEFAULT_CAPTION = "Select a file";
/**
* Shared counter to create identifiers for the calling instance.
*/
private static int CallerTagCount = 0;
private String tag;
private HorizontalPanel composite = new HorizontalPanel();
private Button btnLaunch;
private FileSelectDialog dlgFileSelect;
protected File selectedFile;
protected TextField<String> txtFilename;
protected ClientAction actionChange;
/**
*
*/
public FileSelector()
{
this(null);
}
/**
*
*/
public FileSelector(ClientAction actionChange)
{
this.actionChange = actionChange;
initComponent(composite);
initWidgets();
}
protected void initWidgets()
{
tag = createTag();
composite.setSpacing(9);
txtFilename = new TextField<String>();
txtFilename.setId("idFileName");
txtFilename.setReadOnly(true);
txtFilename.setPosition(-9, 0);
txtFilename.setWidth(254);
btnLaunch = new Button(I18N.DISPLAY.browse());
btnLaunch.setId("idBtnLaunch");
btnLaunch.addListener(Events.OnClick, new Listener<BaseEvent>()
{
public void handleEvent(BaseEvent be)
{
dlgFileSelect = new FileSelectDialog(tag, DEFAULT_CAPTION);
dlgFileSelect.addOkClickHandler(new DialogOkClickHandler()
{
@Override
public void componentSelected(ButtonEvent ce)
{
setSelectedFile(dlgFileSelect.getSelectedFile());
txtFilename.setValue((selectedFile == null) ? "" : selectedFile.getName());
if(actionChange != null)
{
actionChange.execute();
}
}
});
dlgFileSelect.show();
}
});
btnLaunch.focus();
composite.add(txtFilename);
composite.add(btnLaunch);
}
private String createTag()
{
return CALLER_TAG + CallerTagCount++;
}
/**
* Indicates if the widget has a selected file.
*
* @return true, if there is a selection; otherwise false.
*/
public boolean hasSelectedFile()
{
return selectedFile != null;
}
/**
* Retrieves the selected file.
*
* This will return null when a file is not selected.
*
* @return a reference to the selected file or null when there is no selection.
*/
public File getSelectedFile()
{
return selectedFile;
}
/**
* Defines the selected file.
*
* This will all set the selected file in the underlying widgets (including the
* FileSelectDialog)
*
* @param file the selected file
*/
public void setSelectedFile(File file)
{
dlgFileSelect.select(file);
selectedFile = file;
}
/**
* Retrieves an identifier for the file that can be used to fetch metadata or content.
*
* This will return null when a file is not selected.
*
* @return a string representing the identifier for the selected file or null when
* there is no selection.
*/
public String getSelectedFileId()
{
return hasSelectedFile() ? selectedFile.getId() : null;
}
/**
* Sets the internal spacing on the widget's root panel.
*
* @param spacing an integer representing the spacing to use
*/
public void setSpacing(int spacing)
{
composite.setSpacing(spacing);
}
/**
* Sets the caption, or title , of the file selection dialog.
*
* @param caption
*/
public void setDialogCaption(String caption)
{
dlgFileSelect.setCaption(caption);
}
/**
* Sets the text for the button used to launch the file selection dialog.
*
* @param text
*/
public void setButtonText(String text)
{
btnLaunch.setText(text);
}
public void setTextFieldPosition(int left, int top)
{
txtFilename.setPosition(left, top);
}
public void displayFilename(String name)
{
txtFilename.setValue(name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment