Mercurial > dive4elements > river
view flys-client/src/main/java/de/intevation/flys/client/client/ui/ProjectList.java @ 884:ace7e9cfbb7f
Fixed some bugs that avoided datacage usage.
flys-client/trunk@2718 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Tue, 13 Sep 2011 11:31:53 +0000 |
parents | f343b4c2d135 |
children | eeea6a02d62c |
line wrap: on
line source
package de.intevation.flys.client.client.ui; import java.util.Date; import java.util.Map; import java.util.MissingResourceException; import com.google.gwt.core.client.GWT; import com.google.gwt.i18n.client.DateTimeFormat; import com.google.gwt.user.client.Timer; import com.google.gwt.user.client.rpc.AsyncCallback; import com.smartgwt.client.types.Alignment; import com.smartgwt.client.types.ListGridEditEvent; import com.smartgwt.client.types.ListGridFieldType; import com.smartgwt.client.types.SelectionStyle; import com.smartgwt.client.types.SortArrow; import com.smartgwt.client.types.SortDirection; import com.smartgwt.client.util.BooleanCallback; import com.smartgwt.client.util.SC; import com.smartgwt.client.widgets.Canvas; import com.smartgwt.client.widgets.Label; import com.smartgwt.client.widgets.grid.CellFormatter; import com.smartgwt.client.widgets.grid.HoverCustomizer; import com.smartgwt.client.widgets.grid.ListGrid; import com.smartgwt.client.widgets.grid.ListGridField; import com.smartgwt.client.widgets.grid.ListGridRecord; import com.smartgwt.client.widgets.grid.events.CellClickEvent; import com.smartgwt.client.widgets.grid.events.CellClickHandler; import com.smartgwt.client.widgets.grid.events.CellDoubleClickEvent; import com.smartgwt.client.widgets.grid.events.CellDoubleClickHandler; import com.smartgwt.client.widgets.grid.events.EditCompleteEvent; import com.smartgwt.client.widgets.grid.events.EditCompleteHandler; import com.smartgwt.client.widgets.grid.events.RowContextClickEvent; import com.smartgwt.client.widgets.grid.events.RowContextClickHandler; import com.smartgwt.client.widgets.layout.VLayout; import com.smartgwt.client.widgets.menu.Menu; import com.smartgwt.client.widgets.menu.MenuItem; import com.smartgwt.client.widgets.menu.MenuItemSeparator; import com.smartgwt.client.widgets.menu.events.MenuItemClickEvent; import com.smartgwt.client.widgets.menu.events.ClickHandler; import com.smartgwt.client.types.VerticalAlignment; import de.intevation.flys.client.shared.model.Collection; import de.intevation.flys.client.shared.model.CollectionRecord; import de.intevation.flys.client.shared.model.User; import de.intevation.flys.client.client.Config; import de.intevation.flys.client.client.FLYS; import de.intevation.flys.client.client.FLYSConstants; import de.intevation.flys.client.client.event.CollectionChangeEvent; import de.intevation.flys.client.client.event.CollectionChangeHandler; import de.intevation.flys.client.client.services.DeleteCollectionService; import de.intevation.flys.client.client.services.DeleteCollectionServiceAsync; import de.intevation.flys.client.client.services.SetCollectionNameService; import de.intevation.flys.client.client.services.SetCollectionNameServiceAsync; import de.intevation.flys.client.client.services.SetCollectionTTLService; import de.intevation.flys.client.client.services.SetCollectionTTLServiceAsync; import de.intevation.flys.client.client.services.UserCollectionsService; import de.intevation.flys.client.client.services.UserCollectionsServiceAsync; /** * The project list shows a list of projects of a specific user. * * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> */ public class ProjectList extends VLayout implements CollectionChangeHandler, EditCompleteHandler { /** Interval to refresh the user's projects.*/ public static final int UPDATE_INTERVAL = 30000; /** Min Interval to refresh the user's projects.*/ public static final int MIN_UPDATE_INTERVAL = 5000; /** The initial width of this panel.*/ public static final int MIN_WIDTH = 300; /** The max length for new project names.*/ public static final int MAX_NAME_LENGTH = 50; public static final String COLUMN_DATE_WIDTH = "100px"; public static final String COLUMN_TITLE_WIDTH = "*"; public static final String COLUMN_FAVORITE_WIDTH = "75px"; /** The interface that provides i18n messages. */ private FLYSConstants messages = GWT.create(FLYSConstants.class); /** The UserService used to retrieve information about the current user. */ protected UserCollectionsServiceAsync userCollectionsService = GWT.create(UserCollectionsService.class); /** The service used to set the name of a project.*/ protected SetCollectionNameServiceAsync nameService = GWT.create(SetCollectionNameService.class); /** The service used to set the name of a project.*/ protected SetCollectionTTLServiceAsync ttlService = GWT.create(SetCollectionTTLService.class); /** The service used to set the name of a project.*/ protected DeleteCollectionServiceAsync deleteService = GWT.create(DeleteCollectionService.class); /** A pointer to the FLYS instance.*/ protected FLYS flys; /** The user whose projects should be displayed.*/ protected User user; /** The grid that contains the project rows.*/ protected ListGrid grid; /** * The default constructor that creates a new ProjectList for a specific * user. * * @param user The user. */ public ProjectList(FLYS flys, User user) { this.flys = flys; this.user = user; grid = new ListGrid(); initGrid(); init(); initTimer(); grid.addEditCompleteHandler(this); updateUserCollections(); } protected void initGrid() { grid.setWidth100(); grid.setHeight100(); grid.setAutoFitMaxWidth(500); grid.setEmptyMessage(messages.no_projects()); grid.setLoadingDataMessage(messages.load_projects()); grid.setCanEdit(false); grid.setEditEvent(ListGridEditEvent.NONE); grid.setShowHeaderContextMenu(false); grid.setShowSortArrow(SortArrow.NONE); grid.setSortDirection(SortDirection.DESCENDING); grid.setSortField(0); grid.setSelectionType(SelectionStyle.SINGLE); grid.setCanReorderFields(false); ListGridField date = buildDateField(); ListGridField name = buildNameField(); ListGridField fav = buildFavoriteField(); grid.setFields(date, name, fav); // add a handler to set / unset the favorite state of a project grid.addCellClickHandler(new CellClickHandler() { public void onCellClick(CellClickEvent event) { if (event.getColNum() != 2) { return; } CollectionRecord r = (CollectionRecord) event.getRecord(); Collection c = r.getCollection(); c.setTTL(c.getTTL() == 0 ? -1 : 0); updateCollectionTTL(c); } }); // add a handler to open a project grid.addCellDoubleClickHandler(new CellDoubleClickHandler() { public void onCellDoubleClick(CellDoubleClickEvent e) { CollectionRecord record = (CollectionRecord) e.getRecord(); String uuid = record != null ? record.getCollection().identifier() : ""; getFlys().openProject(uuid); } }); // add a handler to open a context menu grid.addRowContextClickHandler(new RowContextClickHandler() { public void onRowContextClick(RowContextClickEvent event) { CollectionRecord record = (CollectionRecord) event.getRecord(); Menu menu = createContextMenu(record); grid.setContextMenu(menu); menu.showContextMenu(); event.cancel(); } }); } /** * Initializes a repeating timer that updates the user's collections. The * interval is specified by the constant <i>UPDATE_INTERVAL</i>. */ protected void initTimer() { Config config = Config.getInstance(); int interval = config.getProjectListUpdateInterval(); interval = interval > MIN_UPDATE_INTERVAL ? interval : UPDATE_INTERVAL; GWT.log("Update project list every " + interval + " milliseconds."); Timer t = new Timer() { @Override public void run() { updateUserCollections(); } }; t.scheduleRepeating(interval); } public FLYS getFlys() { return flys; } /** * Creates a new context menu that interacts with a CollectionRecord. * * @param record The selected record. * * @return the context menu with operations that interact with * <i>record</i>. */ protected Menu createContextMenu(final CollectionRecord record) { Menu menu = new Menu(); MenuItem open = new MenuItem(messages.open_project()); open.addClickHandler(new ClickHandler() { public void onClick(MenuItemClickEvent evt) { getFlys().openProject(record.getCollection().identifier()); } }); MenuItem del = new MenuItem(messages.delete_project()); del.addClickHandler(new ClickHandler() { public void onClick(MenuItemClickEvent evt) { SC.ask(messages.really_delete(), new BooleanCallback() { public void execute(Boolean value) { if (value) { deleteCollection(record.getCollection()); } } }); } }); MenuItem rename = new MenuItem(messages.rename_project()); rename.addClickHandler(new ClickHandler() { public void onClick(MenuItemClickEvent evt) { int row = grid.getRecordIndex(record); grid.startEditing(row, 1, false); } }); menu.addItem(open); menu.addItem(rename); menu.addItem(new MenuItemSeparator()); menu.addItem(del); return menu; } /** * The init() method handles the layout stuff for this widget. */ protected void init() { setWidth(MIN_WIDTH); setMinWidth(MIN_WIDTH); setHeight100(); setShowResizeBar(true); setShowEdges(false); setLayoutMargin(0); setLayoutAlign(VerticalAlignment.TOP); Label title = new Label(messages.projects()); title.setHeight("20"); title.setMargin(5); title.setWidth100(); title.setStyleName("fontLightSmall"); Canvas titleWrapper = new Canvas(); titleWrapper.setStyleName("bgBlueDark"); titleWrapper.setWidth100(); titleWrapper.setHeight("20px"); titleWrapper.addChild(title); Canvas gridWrapper = new Canvas(); gridWrapper.setPadding(0); titleWrapper.setWidth100(); gridWrapper.addChild(grid); addMember(titleWrapper); addMember(gridWrapper); } public void onCollectionChange(CollectionChangeEvent event) { if (event.getOldValue() == null) { updateUserCollections(); } } public void onEditComplete(EditCompleteEvent event) { if (event.getColNum() != 1) { return; } int row = event.getRowNum(); CollectionRecord r = (CollectionRecord) grid.getRecord(row); Collection c = r.getCollection(); Map newValues = event.getNewValues(); String name = (String) newValues.get("name"); int maxLength = getMaxNameLength(); int length = name != null ? name.length() : 0; if (length <= 0 || length > maxLength) { String msg = messages.project_name_too_long(); msg = msg.replace("$LEN", String.valueOf(maxLength)); SC.warn(msg); ListGridRecord[] rs = grid.getRecords(); rs[row] = (ListGridRecord) event.getOldRecord(); grid.setRecords(rs); return; } c.setName(name); updateCollectionName(c); } /** * Set the name of the collection <i>c</i> to a new value. If the update * process succeeded, the project list is refreshed. * * @param c The Collection with a new name. */ protected void updateCollectionName(Collection c) { if (c == null) { return; } GWT.log("Update Collection name: " + c.identifier()); GWT.log("=> New name = " + c.getName()); Config config = Config.getInstance(); nameService.setName(c, config.getServerUrl(), new AsyncCallback<Void>(){ public void onFailure(Throwable caught) { String msg = caught.getMessage(); try { SC.warn(messages.getString(msg)); } catch (MissingResourceException mre) { SC.warn(msg); } } public void onSuccess(Void v) { updateUserCollections(); } }); } /** * Set the ttl of the collection <i>c</i> to a new value. If the update * process succeeded, the project list is refreshed. * * @param c The Collection with a new ttl. */ protected void updateCollectionTTL(Collection c) { if (c == null) { return; } GWT.log("Update Collection TTL: " + c.identifier()); GWT.log("=> New ttl = " + c.getTTL()); Config config = Config.getInstance(); ttlService.setTTL(c, config.getServerUrl(), new AsyncCallback<Void>() { public void onFailure(Throwable caught) { String msg = caught.getMessage(); try { SC.warn(messages.getString(msg)); } catch (MissingResourceException mre) { SC.warn(msg); } } public void onSuccess(Void v) { updateUserCollections(); } }); } /** * Delete the collection <i>c</i>. * * @param c The Collection that should be deleted. */ protected void deleteCollection(final Collection c) { if (c == null) { return; } GWT.log("Delete Collection: " + c.identifier()); Config config = Config.getInstance(); deleteService.delete(c, config.getServerUrl(), new AsyncCallback<Void>(){ public void onFailure(Throwable caught) { String msg = caught.getMessage(); try { SC.warn(messages.getString(msg)); } catch (MissingResourceException mre) { SC.warn(msg); } } public void onSuccess(Void v) { flys.getWorkspace().destroyProject(c.identifier()); updateUserCollections(); } }); } protected void updateUserCollections() { GWT.log("==> ProjectList updates user collections!"); Config config = Config.getInstance(); String url = config.getServerUrl(); String locale = config.getLocale(); userCollectionsService.getUserCollections(url, locale, user.identifier(), new AsyncCallback<Collection[]>() { public void onFailure(Throwable caught) { String msg = caught.getMessage(); try { SC.warn(messages.getString(msg)); } catch (MissingResourceException mre) { SC.warn(msg); } } public void onSuccess(Collection[] collections) { int num = collections != null ? collections.length : 0; GWT.log("Received " + num + " user collections."); updateGrid(collections); } } ); } protected void clearGrid() { ListGridRecord[] records = grid.getRecords(); for (ListGridRecord record: records) { grid.removeData(record); } } protected void updateGrid(Collection[] collections) { clearGrid(); if (collections == null || collections.length == 0) { return; } for (Collection c: collections) { grid.addData(new CollectionRecord(c)); } } public int getMaxNameLength() { return MAX_NAME_LENGTH; } /** * Builds the field in the grid that displays the creation time of a * project. * * @return the grid field. */ protected ListGridField buildDateField() { ListGridField date = new ListGridField( "creationTime", messages.projectlist_creationTime()); date.setType(ListGridFieldType.DATE); date.setCanEdit(false); date.setCellFormatter(new CellFormatter() { public String format(Object value, ListGridRecord rec, int r, int c) { if (value == null) { return null; } DateTimeFormat dtf = DateTimeFormat.getFormat( messages.datetime_format()); return dtf.format((Date)value); } }); date.setWidth(COLUMN_DATE_WIDTH); date.setAlign(Alignment.CENTER); return date; } /** * Builds the field in the grid that displays the name of a project. * * @return the grid field. */ protected ListGridField buildNameField() { ListGridField name = new ListGridField( "name", messages.projectlist_title()); name.setType(ListGridFieldType.TEXT); name.setShowHover(true); name.setHoverCustomizer(new HoverCustomizer() { public String hoverHTML( Object value, ListGridRecord record, int row, int col) { CollectionRecord r = (CollectionRecord) record; Collection c = r.getCollection(); String name = r.getName(); return name != null && name.length() > 0 ? name : c.identifier(); } }); name.setWidth(COLUMN_TITLE_WIDTH); name.setAlign(Alignment.LEFT); return name; } protected ListGridField buildFavoriteField() { ListGridField fav = new ListGridField( "ttl", messages.projectlist_favorite()); fav.setType(ListGridFieldType.IMAGE); String base = GWT.getHostPageBaseURL(); fav.setImageURLPrefix(base + "images/"); fav.setImageURLSuffix(".png"); fav.setWidth(COLUMN_FAVORITE_WIDTH); fav.setAlign(Alignment.CENTER); fav.setCanEdit(false); return fav; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :