BrookIDEIntegration.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. (* _ _
  2. * | |__ _ __ ___ ___ | | __
  3. * | '_ \| '__/ _ \ / _ \| |/ /
  4. * | |_) | | | (_) | (_) | <
  5. * |_.__/|_| \___/ \___/|_|\_\
  6. *
  7. * Microframework which helps to develop web Pascal applications.
  8. *
  9. * Copyright (c) 2012-2020 Silvio Clecio <[email protected]>
  10. *
  11. * Brook framework is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * Brook framework is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with Brook framework; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *)
  25. { Integrates Brook to Delphi or Lazarus IDE. }
  26. unit BrookIDEIntegration;
  27. {$I BrookDefines.inc}
  28. {$IFDEF FPC}
  29. {$WARN 5024 OFF}
  30. {$ENDIF}
  31. interface
  32. uses
  33. SysUtils,
  34. Classes,
  35. TypInfo,
  36. Dialogs,
  37. {$IFDEF LCL}
  38. PropEdits,
  39. ComponentEditors,
  40. {$ELSE}
  41. DesignIntf,
  42. DesignEditors,
  43. ColnEdit,
  44. {$ENDIF}
  45. libsagui,
  46. BrookUtility;
  47. resourcestring
  48. SBrookSelectLibraryTitle = 'Select library ...';
  49. SBrookSharedLibraryFilter = 'Shared libraries (%s)|%s|All files (*.*)|*.*';
  50. SBrookURLEntryPointsEditor = 'URL entry-points editor ...';
  51. SBrookURLRoutesEditor = 'URL routes editor ...';
  52. type
  53. { TBrookLibraryNamePropertyEditor }
  54. TBrookLibraryNamePropertyEditor = class(
  55. {$IFDEF LCL}TFileNamePropertyEditor{$ELSE}TStringProperty{$ENDIF})
  56. public
  57. {$IFDEF LCL}
  58. function GetVerbCount: Integer; override;
  59. function GetVerb(AIndex: Integer): string; override;
  60. procedure ExecuteVerb(AIndex: Integer); override;
  61. {$ENDIF}
  62. function GetFilter: string;{$IFDEF LCL}override{$ELSE}virtual{$ENDIF};
  63. function GetDialogTitle: string;{$IFDEF LCL}override{$ELSE}virtual{$ENDIF};
  64. {$IFNDEF LCL}
  65. function CreateFileDialog: TOpenDialog; virtual;
  66. procedure Edit; override;
  67. function GetAttributes: TPropertyAttributes; override;
  68. function GetDialogOptions: TOpenOptions; virtual;
  69. function GetInitialDirectory: string; virtual;
  70. procedure SetFileName(const AFileName: string); virtual;
  71. {$ENDIF}
  72. end;
  73. { TBrookHTTPRequestMethodsPropertyEditor }
  74. TBrookHTTPRequestMethodsPropertyEditor = class(TSetProperty)
  75. public
  76. procedure GetProperties(AProc:
  77. {$IFDEF LCL}TGetPropEditProc{$ELSE}TGetPropProc{$ENDIF}); override;
  78. end;
  79. { TBrookLibraryNameComponentEditor }
  80. TBrookLibraryNameComponentEditor = class(TDefaultEditor)
  81. public
  82. procedure Edit; override;
  83. function GetVerb(AIndex: Integer): string; override;
  84. {$IFNDEF LCL}
  85. function GetVerbCount: Integer; override;
  86. {$ENDIF}
  87. procedure ExecuteVerb(AIndex: Integer); override;
  88. end;
  89. { TBrookOnMathExtensionComponentEditor }
  90. TBrookOnMathExtensionComponentEditor = class(TDefaultEditor)
  91. protected
  92. procedure EditProperty(const AProperty:
  93. {$IFDEF LCL}TPropertyEditor{$ELSE}IProperty{$ENDIF};
  94. var AContinue: Boolean); override;
  95. end;
  96. { TBrookOnHTTPRequestComponentEditor }
  97. TBrookOnHTTPRequestComponentEditor = class(TDefaultEditor)
  98. protected
  99. procedure EditProperty(const AProperty:
  100. {$IFDEF LCL}TPropertyEditor{$ELSE}IProperty{$ENDIF};
  101. var AContinue: Boolean); override;
  102. end;
  103. { TBrookURLRouterComponentEditor }
  104. TBrookURLRouterComponentEditor = class(TComponentEditor)
  105. public
  106. procedure ExecuteVerb(AIndex: Integer); override;
  107. function GetVerb(AIndex: Integer): string; override;
  108. function GetVerbCount: Integer; override;
  109. end;
  110. { TBrookURLEntryPointsComponentEditor }
  111. TBrookURLEntryPointsComponentEditor = class(TComponentEditor)
  112. public
  113. procedure ExecuteVerb(AIndex: Integer); override;
  114. function GetVerb(AIndex: Integer): string; override;
  115. function GetVerbCount: Integer; override;
  116. end;
  117. {$R BrookTardigradeIcons.res}
  118. procedure Register;
  119. implementation
  120. uses
  121. BrookLibraryLoader,
  122. BrookMathExpression,
  123. BrookMediaTypes,
  124. BrookURLEntryPoints,
  125. BrookURLRouter,
  126. BrookHTTPServer;
  127. {$IFNDEF LCL}
  128. type
  129. TLocalSetElementProperty = class(TSetElementProperty)
  130. public
  131. constructor Create(AParent: TPropertyEditor; AElement: Integer); reintroduce;
  132. end;
  133. constructor TLocalSetElementProperty.Create(AParent: TPropertyEditor;
  134. AElement: Integer);
  135. begin
  136. inherited Create(AParent, AElement);
  137. end;
  138. function BrookHTTPRequestMethodsPropertyMapper(AObj: TPersistent;
  139. APropInfo: PPropInfo): TPropertyEditorClass;
  140. begin
  141. if Assigned(AObj) and (AObj is TBrookURLRoute) and
  142. SameText(APropInfo.NameFld.ToString, 'Methods') then
  143. Exit(TBrookHTTPRequestMethodsPropertyEditor);
  144. Result := nil;
  145. end;
  146. {$ENDIF}
  147. procedure Register;
  148. begin
  149. RegisterComponents('Brook', [
  150. TBrookLibraryLoader,
  151. TBrookMathExpression,
  152. TBrookMIME,
  153. TBrookURLEntryPoints,
  154. TBrookURLRouter,
  155. TBrookHTTPServer
  156. ]);
  157. RegisterPropertyEditor(TypeInfo(TFileName), TBrookLibraryLoader,
  158. 'LibraryName', TBrookLibraryNamePropertyEditor);
  159. {$IFDEF LCL}
  160. RegisterPropertyEditor(TypeInfo(TFileName), TBrookMIME,
  161. 'FileName', TFileNamePropertyEditor);
  162. RegisterPropertyEditor(TypeInfo(string), TBrookHTTPServerSecurity,
  163. 'PrivatePassword', TPasswordStringPropertyEditor);
  164. RegisterPropertyEditor(TypeInfo(TBrookHTTPRequestMethods), nil, '',
  165. TBrookHTTPRequestMethodsPropertyEditor);
  166. RegisterPropertyEditor(TypeInfo(string), TBrookHTTPServer, 'UploadsDir',
  167. TDirectoryPropertyEditor);
  168. {$ELSE}
  169. RegisterPropertyMapper(BrookHTTPRequestMethodsPropertyMapper);
  170. {$ENDIF}
  171. RegisterComponentEditor(TBrookLibraryLoader, TBrookLibraryNameComponentEditor);
  172. RegisterComponentEditor(TBrookMathExpression, TBrookOnMathExtensionComponentEditor);
  173. RegisterComponentEditor(TBrookURLEntryPoints, TBrookURLEntryPointsComponentEditor);
  174. RegisterComponentEditor(TBrookURLRouter, TBrookURLRouterComponentEditor);
  175. RegisterComponentEditor(TBrookHTTPServer, TBrookOnHTTPRequestComponentEditor);
  176. end;
  177. {$IFDEF LCL}
  178. { TBrookLibraryNamePropertyEditor }
  179. function TBrookLibraryNamePropertyEditor.GetVerbCount: Integer;
  180. begin
  181. Result := 1;
  182. end;
  183. function TBrookLibraryNamePropertyEditor.GetVerb(AIndex: Integer): string;
  184. begin
  185. Result := SBrookSelectLibraryTitle;
  186. end;
  187. procedure TBrookLibraryNamePropertyEditor.ExecuteVerb(AIndex: Integer);
  188. begin
  189. Edit;
  190. end;
  191. {$ENDIF}
  192. function TBrookLibraryNamePropertyEditor.GetFilter: string;
  193. var
  194. VSharedSuffix: string;
  195. begin
  196. VSharedSuffix := Concat('*.', SharedSuffix);
  197. Result := Format(SBrookSharedLibraryFilter, [VSharedSuffix,
  198. {$IFDEF LINUX}Concat({$ENDIF}VSharedSuffix
  199. {$IFDEF LINUX}, ';', VSharedSuffix, '.*'){$ENDIF}]);
  200. end;
  201. function TBrookLibraryNamePropertyEditor.GetDialogTitle: string;
  202. begin
  203. Result := SBrookSelectLibraryTitle;
  204. end;
  205. {$IFNDEF LCL}
  206. function TBrookLibraryNamePropertyEditor.CreateFileDialog: TOpenDialog;
  207. begin
  208. Result := TOpenDialog.Create(nil);
  209. end;
  210. procedure TBrookLibraryNamePropertyEditor.Edit;
  211. begin
  212. with CreateFileDialog do
  213. try
  214. Filter := GetFilter;
  215. Options := GetDialogOptions;
  216. FileName := GetStrValue;
  217. InitialDir := GetInitialDirectory;
  218. Title := GetDialogTitle;
  219. if Execute then
  220. SetFileName(FileName);
  221. finally
  222. Free;
  223. end;
  224. end;
  225. function TBrookLibraryNamePropertyEditor.GetAttributes: TPropertyAttributes;
  226. begin
  227. Result := [paDialog, paRevertable];
  228. end;
  229. function TBrookLibraryNamePropertyEditor.GetDialogOptions: TOpenOptions;
  230. begin
  231. Result := [ofEnableSizing];
  232. end;
  233. function TBrookLibraryNamePropertyEditor.GetInitialDirectory: string;
  234. begin
  235. Result := '';
  236. end;
  237. procedure TBrookLibraryNamePropertyEditor.SetFileName(
  238. const AFileName: string);
  239. begin
  240. SetStrValue(AFileName);
  241. end;
  242. {$ENDIF}
  243. { TBrookHTTPRequestMethodsPropertyEditor }
  244. procedure TBrookHTTPRequestMethodsPropertyEditor.GetProperties(
  245. AProc:{$IFDEF LCL}TGetPropEditProc{$ELSE}TGetPropProc{$ENDIF});
  246. var
  247. M: TBrookHTTPRequestMethod;
  248. {$IFNDEF LCL}
  249. P: IProperty;
  250. {$ENDIF}
  251. begin
  252. for M := Succ(Low(TBrookHTTPRequestMethod)) to High(TBrookHTTPRequestMethod) do
  253. {$IFDEF LCL}
  254. AProc(TSetElementProperty.Create(Self, Ord(M)));
  255. {$ELSE}
  256. begin
  257. P := TLocalSetElementProperty.Create(Self, Ord(M));
  258. AProc(P);
  259. P := nil;
  260. end;
  261. {$ENDIF}
  262. end;
  263. { TBrookLibraryNameComponentEditor }
  264. procedure TBrookLibraryNameComponentEditor.Edit;
  265. var
  266. VDialog: TOpenDialog;
  267. VLibraryLoader: TBrookLibraryLoader;
  268. VPropertyEditor: TBrookLibraryNamePropertyEditor;
  269. begin
  270. VLibraryLoader := Component as TBrookLibraryLoader;
  271. if not Assigned(VLibraryLoader) then
  272. Exit;
  273. VPropertyEditor := TBrookLibraryNamePropertyEditor.Create(nil, 0);
  274. VDialog := VPropertyEditor.CreateFileDialog;
  275. try
  276. VDialog.Filter := VPropertyEditor.GetFilter;
  277. VDialog.Options := VPropertyEditor.GetDialogOptions;
  278. VDialog.InitialDir := VPropertyEditor.GetInitialDirectory;
  279. VDialog.Title := VPropertyEditor.GetDialogTitle;
  280. VDialog.FileName := VLibraryLoader.LibraryName;
  281. if VDialog.Execute then
  282. begin
  283. VLibraryLoader.LibraryName := VDialog.FileName;
  284. Designer.Modified;
  285. end;
  286. finally
  287. VPropertyEditor.Free;
  288. end;
  289. end;
  290. function TBrookLibraryNameComponentEditor.GetVerb(
  291. AIndex: Integer): string;
  292. begin
  293. Result := SBrookSelectLibraryTitle;
  294. end;
  295. {$IFNDEF LCL}
  296. function TBrookLibraryNameComponentEditor.GetVerbCount: Integer;
  297. begin
  298. Result := 1;
  299. end;
  300. {$ENDIF}
  301. procedure TBrookLibraryNameComponentEditor.ExecuteVerb(AIndex: Integer);
  302. begin
  303. Edit;
  304. end;
  305. { TBrookOnMathExtensionComponentEditor }
  306. procedure TBrookOnMathExtensionComponentEditor.EditProperty(const AProperty:
  307. {$IFDEF LCL}TPropertyEditor{$ELSE}IProperty{$ENDIF}; var AContinue: Boolean);
  308. begin
  309. if SameText(AProperty.GetName, 'OnExtension') then
  310. inherited EditProperty(AProperty, AContinue);
  311. end;
  312. { TBrookOnHTTPRequestComponentEditor }
  313. procedure TBrookOnHTTPRequestComponentEditor.EditProperty(const AProperty:
  314. {$IFDEF LCL}TPropertyEditor{$ELSE}IProperty{$ENDIF}; var AContinue: Boolean);
  315. begin
  316. if SameText(AProperty.GetName, 'OnRequest') then
  317. inherited EditProperty(AProperty, AContinue);
  318. end;
  319. { TBrookURLRouterComponentEditor }
  320. procedure TBrookURLRouterComponentEditor.ExecuteVerb(AIndex: Integer);
  321. var
  322. VRouter: TBrookURLRouter;
  323. begin
  324. VRouter := GetComponent as TBrookURLRouter;
  325. {$IFDEF LCL}
  326. EditCollection(
  327. {$ELSE}
  328. ShowCollectionEditor(Designer,
  329. {$ENDIF}
  330. VRouter, VRouter.Routes, 'Routes');
  331. end;
  332. function TBrookURLRouterComponentEditor.GetVerb(AIndex: Integer): string;
  333. begin
  334. Result := SBrookURLRoutesEditor;
  335. end;
  336. function TBrookURLRouterComponentEditor.GetVerbCount: Integer;
  337. begin
  338. Result := 1;
  339. end;
  340. { TBrookURLEntryPointsComponentEditor }
  341. procedure TBrookURLEntryPointsComponentEditor.ExecuteVerb(AIndex: Integer);
  342. var
  343. VEntryPoints: TBrookURLEntryPoints;
  344. begin
  345. VEntryPoints := GetComponent as TBrookURLEntryPoints;
  346. {$IFDEF LCL}
  347. EditCollection(
  348. {$ELSE}
  349. ShowCollectionEditor(Designer,
  350. {$ENDIF}
  351. VEntryPoints, VEntryPoints.List, 'List');
  352. end;
  353. function TBrookURLEntryPointsComponentEditor.GetVerb(AIndex: Integer): string;
  354. begin
  355. Result := SBrookURLEntryPointsEditor;
  356. end;
  357. function TBrookURLEntryPointsComponentEditor.GetVerbCount: Integer;
  358. begin
  359. Result := 1;
  360. end;
  361. end.