1+ package com .logicaldoc .gui .frontend .client .ai .model ;
2+
3+ import java .util .ArrayList ;
4+ import java .util .List ;
5+
6+ import com .logicaldoc .gui .common .client .DefaultAsyncCallback ;
7+ import com .logicaldoc .gui .common .client .grid .IdListGridField ;
8+ import com .logicaldoc .gui .common .client .grid .RefreshableListGrid ;
9+ import com .logicaldoc .gui .common .client .i18n .I18N ;
10+ import com .logicaldoc .gui .common .client .util .LD ;
11+ import com .logicaldoc .gui .common .client .widgets .HTMLPanel ;
12+ import com .logicaldoc .gui .common .client .widgets .InfoPanel ;
13+ import com .logicaldoc .gui .frontend .client .ai .AIService ;
14+ import com .smartgwt .client .data .AdvancedCriteria ;
15+ import com .smartgwt .client .data .Record ;
16+ import com .smartgwt .client .types .Alignment ;
17+ import com .smartgwt .client .types .AutoFitWidthApproach ;
18+ import com .smartgwt .client .types .OperatorId ;
19+ import com .smartgwt .client .types .SelectionStyle ;
20+ import com .smartgwt .client .widgets .Canvas ;
21+ import com .smartgwt .client .widgets .grid .ListGrid ;
22+ import com .smartgwt .client .widgets .grid .ListGridField ;
23+ import com .smartgwt .client .widgets .grid .ListGridRecord ;
24+ import com .smartgwt .client .widgets .layout .Layout ;
25+ import com .smartgwt .client .widgets .layout .VLayout ;
26+ import com .smartgwt .client .widgets .menu .Menu ;
27+ import com .smartgwt .client .widgets .menu .MenuItem ;
28+ import com .smartgwt .client .widgets .toolbar .ToolStrip ;
29+ import com .smartgwt .client .widgets .toolbar .ToolStripButton ;
30+
31+ /**
32+ * Panel showing the list of models
33+ *
34+ * @author Marco Meschieri - LogicalDOC
35+ * @since 9.2
36+ */
37+ public class ModelsPanel extends VLayout {
38+
39+ private static final String LABEL = "label" ;
40+
41+ private static final String DESCRIPTION = "description" ;
42+
43+ protected Layout detailsContainer ;
44+
45+ protected RefreshableListGrid list ;
46+
47+ protected Canvas details = SELECT_SAMPLER ;
48+
49+ static final Canvas SELECT_SAMPLER = new HTMLPanel (" " + I18N .message ("selecttasampler" ));
50+
51+ public ModelsPanel () {
52+ setWidth100 ();
53+ }
54+
55+ @ Override
56+ public void onDraw () {
57+ InfoPanel infoPanel = new InfoPanel ("" );
58+
59+ final Layout listing = new VLayout ();
60+ detailsContainer = new VLayout ();
61+ details = SELECT_SAMPLER ;
62+
63+ // Initialize the listing panel
64+ listing .setAlign (Alignment .CENTER );
65+ listing .setHeight ("55%" );
66+ listing .setShowResizeBar (true );
67+
68+ ListGridField id = new IdListGridField ();
69+
70+ ListGridField name = new ListGridField ("name" , I18N .message ("name" ));
71+ name .setCanFilter (true );
72+ name .setCanSort (true );
73+ name .setAutoFit (AutoFitWidthApproach .BOTH );
74+
75+ ListGridField label = new ListGridField (LABEL , I18N .message (LABEL ), 200 );
76+ label .setCanFilter (true );
77+ label .setCanSort (true );
78+
79+ ListGridField description = new ListGridField (DESCRIPTION , I18N .message (DESCRIPTION ), 300 );
80+ description .setCanFilter (true );
81+ description .setCanSort (false );
82+
83+ ListGridField samplerType = new ListGridField ("type" , I18N .message ("type" ));
84+ samplerType .setAutoFit (AutoFitWidthApproach .BOTH );
85+
86+ list = new RefreshableListGrid ();
87+ list .setEmptyMessage (I18N .message ("notitemstoshow" ));
88+ list .setShowAllRecords (true );
89+ list .setAutoFetchData (true );
90+ list .setWidth100 ();
91+ list .setHeight100 ();
92+ list .setFields (id , name , label , samplerType , description );
93+ list .setSelectionType (SelectionStyle .SINGLE );
94+ list .setShowRecordComponents (true );
95+ list .setShowRecordComponentsByCell (true );
96+ list .setCanFreezeFields (true );
97+ list .setFilterOnKeypress (true );
98+ list .setDataSource (new ModelDS ());
99+
100+ listing .addMember (infoPanel );
101+ listing .addMember (list );
102+
103+ ToolStrip toolStrip = new ToolStrip ();
104+ toolStrip .setHeight (20 );
105+ toolStrip .setWidth100 ();
106+ toolStrip .addSpacer (2 );
107+
108+ ToolStripButton refresh = new ToolStripButton ();
109+ refresh .setTitle (I18N .message ("refresh" ));
110+ refresh .addClickHandler (event -> {
111+ list .refresh (new ModelDS ());
112+ detailsContainer .removeMembers (detailsContainer .getMembers ());
113+ details = SELECT_SAMPLER ;
114+ detailsContainer .setMembers (details );
115+ });
116+ toolStrip .addButton (refresh );
117+
118+ ToolStripButton add = new ToolStripButton ();
119+ add .setTitle (I18N .message ("addmodel" ));
120+ toolStrip .addButton (add );
121+ add .addClickHandler (event -> onAddSampler ());
122+
123+ toolStrip .addFill ();
124+
125+ list .addCellContextClickHandler (event -> {
126+ showContextMenu ();
127+ event .cancel ();
128+ });
129+
130+ list .addSelectionChangedHandler (event -> {
131+ Record rec = list .getSelectedRecord ();
132+ if (rec != null )
133+ AIService .Instance .get ().getModel (rec .getAttributeAsLong ("id" ), new DefaultAsyncCallback <>() {
134+ @ Override
135+ public void onSuccess (GUIModel model ) {
136+ showModelDetails (model );
137+ }
138+ });
139+ });
140+
141+ list .addDataArrivedHandler (event -> infoPanel
142+ .setMessage (I18N .message ("showattributesets" , Integer .toString (list .getTotalRows ()))));
143+
144+ detailsContainer .setAlign (Alignment .CENTER );
145+ detailsContainer .addMember (details );
146+
147+ setMembers (toolStrip , listing , detailsContainer );
148+ }
149+
150+ private void showContextMenu () {
151+ Menu contextMenu = new Menu ();
152+
153+ final ListGridRecord [] selection = list .getSelectedRecords ();
154+ List <Long > ids = new ArrayList <>();
155+ for (ListGridRecord rec : selection )
156+ ids .add (rec .getAttributeAsLong ("id" ));
157+
158+ MenuItem delete = new MenuItem ();
159+ delete .setTitle (I18N .message ("ddelete" ));
160+ delete .addClickHandler (event -> LD .ask (I18N .message ("question" ), I18N .message ("confirmdelete" ), confirm -> {
161+ if (Boolean .TRUE .equals (confirm )) {
162+ AIService .Instance .get ().deleteModels (ids , new DefaultAsyncCallback <>() {
163+ @ Override
164+ public void onSuccess (Void result ) {
165+ list .removeSelectedData ();
166+ list .deselectAllRecords ();
167+ showModelDetails (null );
168+ }
169+ });
170+ }
171+ }));
172+
173+ contextMenu .setItems (delete );
174+ contextMenu .showContextMenu ();
175+ }
176+
177+ protected void showModelDetails (GUIModel model ) {
178+ // if (!(details instanceof SamplerDetailsPanel)) {
179+ // detailsContainer.removeMember(details);
180+ // details = new SamplerDetailsPanel(this);
181+ // detailsContainer.addMember(details);
182+ // }
183+ // ((SamplerDetailsPanel) details).setSampler(sampler);
184+ }
185+
186+ public ListGrid getList () {
187+ return list ;
188+ }
189+
190+ /**
191+ * Updates the selected rec with new data
192+ *
193+ * @param model the model to take data from
194+ */
195+ public void updateRecord (GUIModel model ) {
196+ Record rec = list .find (new AdvancedCriteria ("id" , OperatorId .EQUALS , model .getId ()));
197+ if (rec == null ) {
198+ rec = new ListGridRecord ();
199+ // Append a new rec
200+ rec .setAttribute ("id" , model .getId ());
201+ list .addData (rec );
202+ list .selectRecord (rec );
203+ }
204+
205+ rec .setAttribute ("name" , model .getName ());
206+ rec .setAttribute (LABEL , model .getLabel () != null ? model .getLabel () : model .getName ());
207+ rec .setAttribute (DESCRIPTION , model .getDescription ());
208+ list .refreshRow (list .getRecordIndex (rec ));
209+
210+ }
211+
212+ protected void onAddSampler () {
213+ list .deselectAllRecords ();
214+ showModelDetails (new GUIModel ());
215+ }
216+
217+ @ Override
218+ public boolean equals (Object other ) {
219+ return super .equals (other );
220+ }
221+
222+ @ Override
223+ public int hashCode () {
224+ return super .hashCode ();
225+ }
226+ }
0 commit comments