1 {
2  *****************************************************************************
3   See the file COPYING.modifiedLGPL.txt, included in this distribution,
4   for details about the license.
5  *****************************************************************************
6 
7   Author: Mattias Gaertner
8 
9   Abstract:
10     An editor for a list of TCustomTextConverterTool.
11 }
12 unit IDETextConvListEdit;
13 
14 {$mode objfpc}{$H+}
15 
16 interface
17 
18 uses
19   Classes, SysUtils, LCLProc, LResources, Forms, Controls, Graphics, Dialogs,
20   StdCtrls, ClipBrd, Buttons, ExtCtrls,
21   IDETextConverter, ObjectInspector,
22   IDETextConvListAdd, h2passtrconsts;
23 
24 type
25 
26   { TTextConvListEditor }
27 
28   TTextConvListEditor = class(TForm)
29     AddToolButton: TButton;
30     CloneButton: TButton;
31     PasteButton: TButton;
32     CopyToolButton: TButton;
33     MoveToolDownButton: TButton;
34     MoveToolUpButton: TButton;
35     DeleteToolButton: TButton;
36     ToolsSplitter: TSplitter;
37     ToolsPanel: TPanel;
38     ToolsListBox: TListBox;
39     UpDownSplitter: TSplitter;
40     ToolsLabel: TLabel;
41     PropertyGrid: TCustomPropertiesGrid;
42     procedure AddToolButtonClick(Sender: TObject);
43     procedure CloneButtonClick(Sender: TObject);
44     procedure CopyToolButtonClick(Sender: TObject);
45     procedure DeleteToolButtonClick(Sender: TObject);
46     procedure FormCreate(Sender: TObject);
47     procedure MoveToolDownButtonClick(Sender: TObject);
48     procedure MoveToolUpButtonClick(Sender: TObject);
49     procedure PasteButtonClick(Sender: TObject);
50     procedure PropertyGridModified(Sender: TObject);
51     procedure ToolsListBoxSelectionChange(Sender: TObject; User: Boolean);
52   private
53     FListOfTools: TComponent;
54     FModified: boolean;
55     FOnModified: TNotifyEvent;
56     procedure SetListOfTools(const AValue: TComponent);
57     procedure SetModified(const AValue: boolean);
58     procedure MoveSelection(Offset: integer);
GetCurrentToolnull59     function GetCurrentTool: TCustomTextConverterTool;
60     procedure MakeToolCaptionAndNameUnique(NewTool: TCustomTextConverterTool);
61   public
62     procedure SelectTool(Tool: TCustomTextConverterTool);
CreateToolnull63     function CreateTool(ToolClass: TCustomTextConverterToolClass
64                         ): TCustomTextConverterTool;
65     procedure UpdateAll;
66     procedure UpdateToolsListBox;
67     procedure UpdateButtons;
68     property ListOfTools: TComponent read FListOfTools write SetListOfTools;
69     property Modified: boolean read FModified write SetModified;
70     property OnModified: TNotifyEvent read FOnModified write FOnModified;
71   end;
72 
73 var
74   TextConvListEditor: TTextConvListEditor;
75 
76 
77 implementation
78 
79 {$R idetextconvlistedit.lfm}
80 
81 { TTextConvListEditor }
82 
83 procedure TTextConvListEditor.FormCreate(Sender: TObject);
84 begin
85   Caption := h2pTextConversionToolsEditor;
86   ToolsLabel.Caption := h2pTools;
87 
88   // buttons
89   AddToolButton.Caption := h2pAddNewTool;
90   CloneButton.Caption := h2pAddACopy;
91   PasteButton.Caption := h2pAddFromClipboard;
92   CopyToolButton.Caption := h2pCopyToolToClipboard;
93   MoveToolDownButton.Caption := h2pMoveDown;
94   MoveToolUpButton.Caption := h2pMoveUp;
95   DeleteToolButton.Caption := h2pDeleteTool;
96 
97   PropertyGrid:=TCustomPropertiesGrid.Create(Self);
98   PropertyGrid.Align:=alBottom;
99   PropertyGrid.AnchorToNeighbour(akTop,0,UpDownSplitter);
100   PropertyGrid.Parent:=Self;
101   PropertyGrid.OnModified:=@PropertyGridModified;
102 
103   UpdateButtons;
104 end;
105 
106 procedure TTextConvListEditor.MoveToolDownButtonClick(Sender: TObject);
107 begin
108   MoveSelection(+1);
109 end;
110 
111 procedure TTextConvListEditor.MoveToolUpButtonClick(Sender: TObject);
112 begin
113   MoveSelection(-1);
114 end;
115 
116 procedure TTextConvListEditor.PasteButtonClick(Sender: TObject);
117 var
118   NewComponent: TComponent;
119   NewTool: TCustomTextConverterTool;
120 begin
121   if FListOfTools=nil then exit;
122   try
123     NewComponent:=nil;
124     Clipboard.GetComponentAsText(NewComponent,
125                               @TextConverterToolClasses.FindClass,FListOfTools);
126     if NewComponent=nil then
127       raise Exception.Create('nil');
128     if not (NewComponent is TCustomTextConverterTool) then begin
129       NewComponent.Free;
130       raise Exception.Create(h2pNotATCustomTextConverterTool);
131     end;
132     NewTool:=TCustomTextConverterTool(NewComponent);
133     MakeToolCaptionAndNameUnique(NewTool);
134     Modified:=true;
135     UpdateToolsListBox;
136     SelectTool(NewTool);
137   except
138     on E: Exception do begin
139       MessageDlg(h2pError,
140         Format(h2pErrorConvertingClipboardTextToTextTool, [#13, E.Message]), mtError, [mbCancel], 0);
141     end;
142   end;
143 end;
144 
145 procedure TTextConvListEditor.PropertyGridModified(Sender: TObject);
146 var
147   Tool: TCustomTextConverterTool;
148 begin
149   Tool:=GetCurrentTool;
150   //DebugLn(['TTextConvListEditor.PropertyGridModified ',dbgsName(Tool)]);
151   if Tool=nil then exit;
152   MakeToolCaptionAndNameUnique(Tool);
153   Modified:=true;
154 end;
155 
156 procedure TTextConvListEditor.ToolsListBoxSelectionChange(Sender: TObject;
157   User: Boolean);
158 var
159   Tool: TCustomTextConverterTool;
160 begin
161   if User then ;
162   if csDestroying in ComponentState then exit;
163   UpdateButtons;
164   Tool:=GetCurrentTool;
165   //DebugLn(['TTextConvListEditor.ToolsListBoxSelectionChange Tool=',dbgsName(Tool)]);
166   PropertyGrid.TIObject:=Tool;
167 end;
168 
169 procedure TTextConvListEditor.AddToolButtonClick(Sender: TObject);
170 var
171   ToolClass: TCustomTextConverterToolClass;
172 begin
173   if FListOfTools=nil then exit;
174   if ShowIDETextConvListAddDlg(ToolClass)<>mrOk then exit;
175   CreateTool(ToolClass);
176 end;
177 
178 procedure TTextConvListEditor.CloneButtonClick(Sender: TObject);
179 var
180   Tool: TCustomTextConverterTool;
181   NewTool: TCustomTextConverterTool;
182 begin
183   Tool:=GetCurrentTool;
184   if Tool=nil then exit;
185   NewTool:=TCustomTextConverterToolClass(Tool.ClassType).Create(FListOfTools);
186   NewTool.Assign(Tool);
187   MakeToolCaptionAndNameUnique(NewTool);
188   Modified:=true;
189   UpdateToolsListBox;
190   SelectTool(NewTool);
191 end;
192 
193 procedure TTextConvListEditor.CopyToolButtonClick(Sender: TObject);
194 var
195   Tool: TCustomTextConverterTool;
196 begin
197   Tool:=GetCurrentTool;
198   if Tool=nil then exit;
199   try
200     Clipboard.SetComponentAsText(Tool);
201   except
202     on E: Exception do begin
203       MessageDlg(h2pError,
204         Format(h2pErrorConvertingPuttingToolOntoClipboard, [#13, E.Message]), mtError, [mbCancel], 0);
205     end;
206   end;
207 end;
208 
209 procedure TTextConvListEditor.DeleteToolButtonClick(Sender: TObject);
210 var
211   Tool: TCustomTextConverterTool;
212   i: LongInt;
213 begin
214   Tool:=GetCurrentTool;
215   if Tool=nil then exit;
216   if QuestionDlg(h2pConfirmDelete,
217     Format(h2pDoYouReallyWantToDelete, [Tool.Caption]),
218     mtConfirmation, [mrYes, h2pDelete, mrCancel], 0
219     )<>mrYes
220   then exit;
221   i:=ToolsListBox.ItemIndex;
222   PropertyGrid.TIObject:=nil;
223   Tool.Free;
224   Modified:=true;
225   UpdateToolsListBox;
226   if i>=ToolsListBox.Items.Count then
227     i:=ToolsListBox.Items.Count-1;
228   ToolsListBox.ItemIndex:=i;
229 end;
230 
231 procedure TTextConvListEditor.SetListOfTools(const AValue: TComponent);
232 begin
233   if (FListOfTools=AValue) then exit;
234   FListOfTools:=AValue;
235   PropertyGrid.TIObject:=nil;
236   UpdateAll;
237 end;
238 
239 procedure TTextConvListEditor.SetModified(const AValue: boolean);
240 begin
241   if FModified=AValue then exit;
242   FModified:=AValue;
243   if FModified and Assigned(OnModified) then OnModified(Self);
244 end;
245 
246 procedure TTextConvListEditor.UpdateAll;
247 begin
248   UpdateToolsListBox;
249   UpdateButtons;
250 end;
251 
252 procedure TTextConvListEditor.UpdateToolsListBox;
253 var
254   sl: TStringList;
255   i: Integer;
256   Tool: TCustomTextConverterTool;
257   OldSelected: String;
258 begin
259   sl:=TStringList.Create;
260   if FListOfTools<>nil then begin
261     for i:=0 to FListOfTools.ComponentCount-1 do begin
262       Tool:=FListOfTools.Components[i] as TCustomTextConverterTool;
263       sl.Add(Tool.Caption);
264       //DebugLn(['TTextConvListEditor.UpdateToolsListBox Caption=',Tool.Caption,' ',dbgsName(Tool)]);
265     end;
266   end;
267   //DebugLn(['TTextConvListEditor.UpdateToolsListBox ',sl.Count,' "',sl.Text,'"']);
268   // save selection
269   OldSelected:='';
270   if ToolsListBox.ItemIndex>=0 then
271     OldSelected:=ToolsListBox.Items[ToolsListBox.ItemIndex];
272   // commit new list
273   ToolsListBox.Items.Assign(sl);
274   // restore selection
275   if OldSelected<>'' then
276     ToolsListBox.ItemIndex:=ToolsListBox.Items.IndexOf(OldSelected);
277   sl.Free;
278 end;
279 
280 procedure TTextConvListEditor.UpdateButtons;
281 var
282   i: LongInt;
283 begin
284   i:=ToolsListBox.ItemIndex;
285   DeleteToolButton.Enabled:=(i>=0);
286   MoveToolDownButton.Enabled:=(i<ToolsListBox.Items.Count-1);
287   MoveToolUpButton.Enabled:=(i>0);
288   CloneButton.Enabled:=(i>=0);
289   PasteButton.Enabled:=true;
290   CopyToolButton.Enabled:=(i>=0);
291 end;
292 
293 procedure TTextConvListEditor.MoveSelection(Offset: integer);
294 var
295   i: LongInt;
296   Tool: TCustomTextConverterTool;
297 begin
298   if FListOfTools=nil then exit;
299   if Offset=0 then exit;
300   i:=ToolsListBox.ItemIndex;
301   if (i>=0) and (i<FListOfTools.ComponentCount)
302   and (i+Offset>=0) and (i+Offset<FListOfTools.ComponentCount) then begin
303     Tool:=FListOfTools.Components[i] as TCustomTextConverterTool;
304     Tool.ComponentIndex:=Tool.ComponentIndex+Offset;
305     Modified:=true;
306     UpdateToolsListBox;
307   end;
308 end;
309 
TTextConvListEditor.GetCurrentToolnull310 function TTextConvListEditor.GetCurrentTool: TCustomTextConverterTool;
311 var
312   i: LongInt;
313 begin
314   Result:=nil;
315   if FListOfTools=nil then exit;
316   i:=ToolsListBox.ItemIndex;
317   //DebugLn(['TTextConvListEditor.GetCurrentTool ',dbgsName(Self),' ToolsListBox.ItemIndex=',ToolsListBox.ItemIndex,' FListOfTools.ComponentCount=',FListOfTools.ComponentCount]);
318   if (i<0) or (i>=FListOfTools.ComponentCount) then exit;
319   Result:=TCustomTextConverterTool(FListOfTools.Components[i]);
320 end;
321 
322 procedure TTextConvListEditor.MakeToolCaptionAndNameUnique(
323   NewTool: TCustomTextConverterTool);
324 var
325   i: Integer;
326 begin
327   MakeToolNameUnique(FListOfTools,NewTool);
328   MakeToolCaptionUnique(FListOfTools,NewTool);
329   if (FListOfTools<>nil) then begin
330     for i:=0 to FListOfTools.ComponentCount-1 do begin
331       if FListOfTools.Components[i]=NewTool then begin
332         if (i<ToolsListBox.Items.Count) then begin
333           ToolsListBox.Items[i]:=NewTool.Caption;
334         end;
335       end;
336     end;
337   end;
338 end;
339 
340 procedure TTextConvListEditor.SelectTool(Tool: TCustomTextConverterTool);
341 var
342   i: LongInt;
343 begin
344   if FListOfTools=nil then exit;
345   if Tool.Owner<>FListOfTools then exit;
346   i:=Tool.ComponentIndex;
347   if (i<=0) or (i>=ToolsListBox.Items.Count) then exit;
348   ToolsListBox.ItemIndex:=i;
349 end;
350 
TTextConvListEditor.CreateToolnull351 function TTextConvListEditor.CreateTool(ToolClass: TCustomTextConverterToolClass
352   ): TCustomTextConverterTool;
353 begin
354   Result:=nil;
355   if FListOfTools=nil then exit;
356   Result:=AddNewTextConverterTool(FListOfTools,ToolClass);
357   Modified:=true;
358   UpdateToolsListBox;
359   SelectTool(Result);
360 end;
361 
362 end.
363 
364