Jump to content
PeterPanettone

Extract selected UI and code elements to insert them into another project

Recommended Posts

In the IDE's FormDesigner Structure panel, when I copy a number of selected components, I get a collection of objects in the clipboard.

 

An IDE expert would search and extract all code elements in the project's source code referring to any of the objects in the collection.

 

This would allow us to easily copy a selection of UI elements together with their inherent code functionality and insert them into another project with minimal effort.

 

The steps to extract the code elements would be:

 

1. Create a list of the object names in the clipboard collection

 

2. Search those object names in the project's source code

 

3. Automate the extraction of the code elements (procedures, functions, declarations, variables, etc.) referring objects in the object collection and put them in a code collection.

 

4. Insert the objects and the code-elements into another project.

 

5. Make the necessary adjustments and corrections

Share this post


Link to post

Probably that doesn't help you much right now, but

I usually decompose more complex views into smaller modules, already before putting all components on one form.


To have a few separate views, which show specific aspects of a whole view, like media.play.controller;  media.play.playlist, media.play.viewer,

all were placed into separate, selfcontaining frames or forms.

In my final views I can simple compose them, and connect them via interface bindings and/or messages.

 

Thats way makes it easier to test, compose, re-use and re-place separate parts of a projects view.

 

All it needs is a little more discipline before clicking such complex views or datamodules together, and a little more efford,

but I'm not ready either to do this always right, especially when I needed a "fast" solution again :classic_sad:

 

Edited by Rollo62
  • Like 1

Share this post


Link to post
20 minutes ago, Rollo62 said:

all were placed into separate, selfcontaining frames or forms.

I have to make some more research into separating different functionalities in confined areas where they can be easily reused. It would be nice if the IDE (or an IDE expert) could help us doing this or help us refactoring existing code into clearly separated code areas.

Edited by PeterPanettone

Share this post


Link to post

When it comes to IDE experts, I'm a little careful, as I prefer the manual way to not rely on some experts or IDE "magic" too much.

After separating the number of components is reduced into a few frames to handle, and they easily can be  handled manually.

Of coarse I prepare my views manually at runtime too, putting them into the right container, not in the IDE designer, which might cause all sorts of issues.

 

That way the RAD in RadStudio becomes very handy for the frame/form design, but for the rest I try to rely on coding over drag-n-drop.

For me this is the best of both worlds.

Edited by Rollo62

Share this post


Link to post

Consider this case: You have a PageControl where each page contains a number of UI-controls so that each page has a separate functionality. It is easy to re-use a specific functionality by copying the UI controls from a specific page. But how would you a priori separate just the code connected to these UI-controls so that just this code could be easily re-used?

 

It would help if it was possible to put a TForm on a PageControl page. In this way both the UI-controls and their connected code would be confined inside this form. I diffusely remember a UI-component allowing to do this: Placing a TForm in it.

Edited by PeterPanettone

Share this post


Link to post

To write an expert that helps separate the UI from the logic would be far from trivial. 

 

I strive to write code that is testable, something that Steve McConnell's "Code Complete" drilled into me. 


My logic routines and biz objects are created without a UI, and the UI itself is connected afterwards.  

I make an effort to isolate the displayed data from the data that is actually used. 

I.e. a grid only reflects the underlying data, and all work is done on the underlying data and not the data in the grid itself. 

A checkbox state reflects the underlying state, and a change to the checkbox affects the underlying data - which then again applies any recalculations to states, and suggest a refresh of the display.

 

The benefits: It is easy to write unit tests and integration tests for the biz logic.  It is easy to reuse the code, or change the UI.  It also simplfies changing the biz.logic as it is inside the "black box" object, and not heavily tied to the UI. If the tests says the object works, and the app doesn't work - the error is in the coupling between the non-visual object(s) and the UI.


The drawback: There is a certain degree of data duplication - but the cost of that is quite limited.

 

You can use proxy classes and/or Attributes/RTTI to associate elements between the non-visual and UI elements.


Divide and conquer is good - but hard unless applied from the start on.

  • Like 1

Share this post


Link to post

The task to extract the controls from a tab sheet or other part of a form into a frame is quite common. A requirement is the code affecting those controls being orthogonal in the first place. The I am able to copy all relevant properties, methods and event handlers from the form class into the frame class. After that I can copy the controls from the form designer into the frame. This usually keeps the event handlers wired. After verifying that everything still compiles I am able to remove all those properties, methods and controls from the original form and clear the event handlers so that they will be removed. All this are about three or four steps when using MMX.

 

I am pretty sure that writing an expert able to do all this automatically will be pretty complicated and error prone. It might work for some simple cases, but probably not for medium sophisticated code.

  • Like 1

Share this post


Link to post
10 minutes ago, Uwe Raabe said:

orthogonal

I know the term "orthogonal" from geometry. What do you mean with "orthogonal" in respect of code?

Share this post


Link to post
6 hours ago, PeterPanettone said:

What do you mean with "orthogonal" in respect of code?

Code that doesn't interact, so it can be split in separate parts without change.

 

In the context above that means, one can identify properties, fields and methods in a class that can live on their own. If you delete all of these, the rest of the code is still compiling. Also vice versa, if you delete all the other code it still compiles.

 

You  can achieve the same result by just copying the class and then deleting one part in each.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×